SQL 08按行排列和分区筛选

时间:2015-02-12 19:20:28

标签: sql-server-2008

***帮助****这可能是重复但我已经搜索了答案,但无法得出任何结果。我是为了寻找一点帮助而做到这一点......

基本上,我有一个查询可以产生我的行排名并且工作得很好但是我不能按地址排名过滤...

寻找一排。任何帮助,将不胜感激。  结果:

 Andress rank      aDDRESS               PO.primOffInd       pIDKEY         
 1                  100 N WEST                 Y               1
 2                  300 N WEST                 N               1
 3                  500 N WEST                 N               1 
 4                  600 WEST                   N               1   

想:下面的代码给我地址排名和4行我需要在下面显示的内容。这是针对行的,所以我希望能够按地址等级进行过滤

 Address rank   Address       PO.primOffInd    Pikdey
 1              100 N WEST      Y               1

2 个答案:

答案 0 :(得分:0)

SELECT *
FROM
    (
    SELECT DISTINCT RANK() OVER
        (PARTITION BY A, PO ORDER BY PO, A, i, c, d, l, UI.UNIQUEID) AS AddressRank /* actual column names were obfuscated for security considerations */
        ... /* the rest of the query minus the order by clause */
    ) as T
where AddressRank = 1
order by uniqueId desc /* ui is not a valid alias here so I removed it */

顺便说一下,我假设你想要的其他列在查询中可用。我不认为DISTINCT正在添加任何值,因为(我收集)排名列可能使所有行都是唯一的。我建议删除它。

答案 1 :(得分:0)

如果您要做的就是过滤掉rank = 1的记录,您可以将查询包装在另一个查询中并使用where子句:

SELECT * FROM (
  -- insert your query that generates the ranking here...
) AS t WHERE t.AddressRank = 1