簡體   English   中英

在基於子級的查詢中選擇前n個父級

[英]Select top n parent in a child based query

我想限制此基於子表的查詢返回的父母數。

SELECT
    ch.Id AS childId,
    pr.Id AS parentId
FROM childTable ch
INNER JOIN parentTable pr ON ch.Id = pr.Id
WHERE
    1 = 1
ORDER BY pr.CreatedDate DESC

這應該是一個普遍的需求,但是我找不到合適的詞來描述它。

我必須返回由where語句過濾的子記錄,但要限制查詢返回的父記錄數。

就像,讓我將孩子記錄保存在....,用父創建日期對它們進行排序,並且僅獲取前n個父記錄。

我試圖獲取行號,但它計算了孩子的數量

 ...ROW_NUMBER() OVER (PARTITION BY pr.Id ORDER BY pr.CreatedDate DESC) AS Row,
...

我嘗試按父項對記錄進行分組,但隨后我丟失了子表中所需的詳細信息。

我使用MS-SQL 2008

編輯

這是演示

有關演示的說明: PostAttachments是我之前提到的子表 ,而PostsParent

Contents表剛剛加入了where語句,並從該表中獲取了一些列。 但是值得將其添加到演示中,以便ContentId作為PostAttachments中Composite鍵的一部分

編輯

前2個帖子的預期輸出,其中(PostAttachment.xxxx = xxxx):

說以下列表中的第(1,3,5,7,8,9)行的where語句正確...

(行僅用於解釋說明,不在數據庫上)

[Line]  POSTID  CONTENTID   CREATEDDATE                         TITLE   CONTENTTITLE
1       1       1           September, 25 2014 09:22:27+0000    post1   content1
2       2       2           September, 25 2014 09:22:27+0000    post2   content2
3       3       3           September, 25 2014 09:22:27+0000    post3   content3
4       4       4           September, 25 2014 09:22:27+0000    post4   content4
5       5       5           September, 25 2014 09:22:27+0000    post5   content5
6       1       6           September, 25 2014 09:22:27+0000    post1   content6
7       2       7           September, 25 2014 09:22:27+0000    post2   content7
8       3       8           September, 25 2014 09:22:27+0000    post3   content8
9       4       9           September, 25 2014 09:22:27+0000    post4   content9

因此結果將具有PostId為1,3,5,2,3,4的行,但我只希望其中的前2個按TITLE排序

在這種情況下的預期輸出應為post1和post2 ...

[Line]  POSTID  CONTENTID   CREATEDDATE                         TITLE   CONTENTTITLE
1       1       1           September, 25 2014 09:22:27+0000    post1   content1
2       2       2           September, 25 2014 09:22:27+0000    post2   content2
6       1       6           September, 25 2014 09:22:27+0000    post1   content6
7       2       7           September, 25 2014 09:22:27+0000    post2   content7

文藝青年最愛的; 我需要在此演示中按語句減少多余的位置和順序

我想這就是你想要的。

SELECT
    pr.PostId AS POSTID
    ,Contents.ContentId
    ,ch.CreatedDate
    ,pr.Title AS Title
    ,Contents.ContentTitle
FROM PostAttachments ch
INNER JOIN Posts pr ON ch.PostId = pr.PostId
INNER JOIN Contents ON ch.ContentId = Contents.ContentId
INNER JOIN (
    SELECT TOP 2 PostId
    FROM PostAttachments
    GROUP BY PostId
    ) t ON ch.PostId = t.PostId

還對您的小提琴進行了測試。

編輯

根據進一步的討論。 在小提琴中檢查以下查詢。

SELECT TOP 4 *
FROM (
    SELECT pr.PostId AS POSTID
        ,Contents.ContentId
        ,ch.CreatedDate
        ,pr.Title AS Title
        ,Contents.ContentTitle
        ,Contents.ContentType
    FROM PostAttachments ch
    INNER JOIN Posts pr ON ch.PostId = pr.PostId
    INNER JOIN Contents ON ch.ContentId = Contents.ContentId
    INNER JOIN (
        SELECT PostId
        FROM PostAttachments
        GROUP BY PostId
        ) t ON ch.PostId = t.PostId
    ) a
WHERE a.ContentType = 0
ORDER BY a.Title DESC
    ,a.ContentId ASC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM