如何查询相同的col1,col2,col3作为排名1

时间:2018-01-25 01:43:03

标签: sql sql-server

我有4列看起来像这样

<div class="SocialBar">Visible</div>
<div class="SocialBar PersonalHome-interests u-mbg">Hidden</div>

我想做的是。根据他们的Timestamp | Last Name | Middle name | First Name| CSP 2018-01-25 09:20:55.000 Santos Enriquez Marco 1 2018-01-25 09:20:55.000 Santos Enriquez Marco 1 2018-01-25 09:20:55.000 Ordiz Jr. Bucoladlan Nulf 1 2018-01-25 09:20:55.000 Urbiztondo Aguillion Bernardo 2

对他们进行排名

预期输出

Last Name,Middle Name,First Name

我尝试使用Rank(),但它

Rank                    | Last Name         | Middle name         | First Name|
1                         Santos              Enriquez              Marco
1                         Santos              Enriquez              Marco
2                         Ordiz Jr.           Bucoladlan            Nulf
1                         Urbiztondo          Aguillion             Bernardo

但是我知道它的错误只是为所有行返回SELECT RANK() OVER(PARTITION BY [Last Name],[Middle Name],[First Name] ORDER BY [Last Name]) RN, [Last Name], [Middle Name], [First Name] FROM [IROAv2].[dbo].[AgentMainTbl] WHERE [First Name] != 'Arnulfo' ORDER BY [Last Name]

1 个答案:

答案 0 :(得分:2)

您想要dense_rank,只需要order by

SELECT DENSE_RANK() OVER (ORDER BY [Last Name], [Middle Name], [First Name]) RN,
      [Last Name], [Middle Name],[First Name]
FROM [IROAv2].[dbo].[AgentMainTbl]
WHERE [First Name] <> 'Arnulfo'
ORDER BY [Last Name];

PARTITION BY按键分区的每个新值重新开始编号。您似乎希望在整个表格上连续编号,相同键的三元组具有相同的数字。