如何根据唯一字段获取行号并按其他字段排序

时间:2013-06-03 10:27:20

标签: sql-server sql-server-2008-r2

我希望使用MS SQL获得此类结果

PAGE 1
ID     Research Title       Published Date    Author ID   Author Name
1      Research A           2013-5-10          D567           John
1      Research A           2013-5-10          D568           Stan
2      Research B           2013-5-9           D569           Steve
3      Research C           2013-5-8           D570           Michael

PAGE 2
ID     Research Title       Published Date    Author ID   Author Name
4      Research D           2013-5-8           D567           John
5      Research F           2013-5-7           D570           Michael
5      Research F           2013-5-7           D568           Stan    
5      Research F           2013-5-7           D569           Steve    
6      Research E           2013-5-6           D569           Steve

结果是3表

的组合

包含所有研究数据的表“研究”

------ TABLE Research------
- ResearchID Uniqueidentifier
- ResearchTitle text
- PublishedDate

表“作者”包含所有讲师数据

------ TABLE Author ------
- AuthorID varchar(20)
- AuthorName vachar(100)

并且,创建“研究”和“作者”之间关系的表“ResearchAuthorMapping”

------ TABLE ResearchAuthorMapping------
- ResearchID uniqueidentifier    
- AuthorID varchar(20)

到目前为止,这是我的查询

DECLARE @offset int;
DECLARE @limit int = 3;
DECLARE @page int = 1 -- CHANGE BY REQUEST
SET @offset = ((@page - 1) * @limit) + 1;
SELECT * 
FROM
(
    SELECT
        DENSE_RANK() OVER(ORDER BY Research.ResearchTitle DESC) AS num,
        Research.*
    FROM
        Research
        LEFT JOIN
            ResearchAuthorMapping ON Research.ResearchID = ResearchAuthorMapping.ResearchID
        LEFT JOIN
            Author ON ResearchAuthorMapping.AuthorID = Author.AuthorID

)TempTable
WHERE
    TempTable.num >= @Offset AND TempTable.num < @offset + @limit

到现在为止,我只能实现这个目标

PAGE 1
ID     Research Title       Published Date    Author ID   Author Name
1      Research F           2013-5-7           D570           Michael
1      Research F           2013-5-7           D568           Stan
1      Research F           2013-5-7           D569           Steve    
2      Research E           2013-5-6           D569           Steve    
3      Research D           2013-5-8           D567           John

PAGE 2
ID     Research Title       Published Date    Author ID   Author Name
4      Research C           2013-5-8           D570           Michael
5      Research B           2013-5-9           D569           Steve
6      Research A           2013-5-10          D567           John
6      Research A           2013-5-10          D568           Stan

由Research Title,

订购的结果集

但是,我想要的是按发布日期排序的数据,但是根据

获取ID

ResearchTitle / ResearchID分组(在我的查询中,我使用DENSE_RANK获取ID)。 我使用ID来限制结果(分页)

*抱歉我的英文不好

1 个答案:

答案 0 :(得分:2)

我认为您可以将结果包装在row_number集中,该行重新排序初始结果。有点像:

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY PublishedDate ) AS rownum
                   ,A.*
          FROM      ( SELECT    DENSE_RANK() OVER ( ORDER BY Research.ResearchTitle DESC ) AS num
                               ,ResearchID
                               ,PublishedDate
                      FROM      Research ) A ) B
LEFT JOIN ResearchAuthorMapping C
ON      B.ResearchID = C.ResearchID
LEFT JOIN Author D
ON      C.AuthorID = D.AuthorID
WHERE   B.rownum >= @Offset
        AND B.rownum < @offset + @limit

我将连接移动到子查询之外以限制查找。