如何通过非选定列进行排序(ROW_NUMBER()超过分区)

时间:2015-12-08 04:56:33

标签: sql-server sql-order-by common-table-expression row-number

我的查询为:

WITH Top_Areas AS
(
SELECT AIP.aid [Author_ID], CAST(P.research_area as VARCHAR(100)) [Research_Area], 
       P.research_area_category_id [Category],
       COUNT(*) [Paper_Count],  
       P.p_year [Paper_Year]
FROM coauthor_individual_papers AIP
JOIN sub_aminer_paper P ON AIP.pid = P.pid
WHERE AIP.aid IN (901706) AND AIP.p_year IN (2005)
GROUP BY
    AIP.aid, CAST(P.research_area as VARCHAR(100)),
    P.research_area_category_id, 
    P.p_year
),
Top_Author_Areas AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY Author_ID, Paper_Year ORDER BY Paper_Count DESC, Research_Area ASC) AS Rank FROM Top_Areas)

SELECT * 
FROM CTE1
WHERE Rank <= 5  

,输出为:

Author_ID   Research_Area   Category    Paper_Count Paper_Year  Rank  
--------------------------------------------------------------------
901706      feature         8           1           2005        1
901706      image annotate  11          1           2005        2
901706      image db        11          1           2005        3
901706      retrieval model 12          1           2005        4  

这个输出只是Research_AreaASC的排序,而我还需要Paper_CountDESC,而所需的输出应该如:

Author_ID   Category    Paper_Count Paper_Year  Rank  
----------------------------------------------------
901706      11          2           2005        1
901706      8           1           2005        2
901706      12          1           2005        3  

我们可以在Research_Area列表中选择SELECT,但如果我们不选择Research_Area,那么如何ORDER BY Research_Area ASC,而我们要使用ORDER BY Research_Area ASCPaper_Count值与上面所需输出中显示的相同时,{1}}选项即为 由于Category的最高值,Rank 11位于Paper_Count 1 Category 8和11分别位于Rank 2和3,原因如下:
- Paper_Count值相同。
- 所以必须按Rank

订购Research_Area ASC 2和3

我们如何获得所需的输出?
请帮忙。

1 个答案:

答案 0 :(得分:2)

因为当我们尝试实施您的打破平局规则时,您似乎仍然不清楚我的观点是否存在可能不同的Research_Area值,我和#39; ll任意决定 - 我们会在每个Research_Area中使用最早的Category值来打破平局:

WITH Top_Areas AS
(
SELECT AIP.aid [Author_ID],

  MIN(CAST(P.research_area as VARCHAR(100))) [Research_Area],

       P.research_area_category_id [Category],
       COUNT(*) [Paper_Count],  
       P.p_year [Paper_Year]
FROM coauthor_individual_papers AIP
JOIN sub_aminer_paper P ON AIP.pid = P.pid
WHERE AIP.aid IN (901706) AND AIP.p_year IN (2005)
GROUP BY
    AIP.aid,

    --CAST(P.research_area as VARCHAR(100)),

    P.research_area_category_id, 
    P.p_year
),
Top_Author_Areas AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY Author_ID, Paper_Year
                             ORDER BY Paper_Count DESC, Research_Area ASC) AS Rank
 FROM Top_Areas)

SELECT * 
FROM CTE1
WHERE Rank <= 5

更改的区域位于与查询其余部分分开的行上的空行 - 我只是将MIN应用于Research_Area以获取单个值用于打破平局,并将其从GROUP BY移除Top_Areas CTE中的两个更改。