如何按一列排序,但根据非数字的不同列进行排名?
How to order by one column, but rank based on a different column that is not numeric?
我有四列要排名。他们需要按员工 ID 分组,然后按订单号从低到高列出。然后,当一切都井井有条时,我真的在努力获得城市在该顺序中的排名。如果同一个员工在同一个城市中依次列出,那么我希望那些排名相同的城市。
下面是 table 的示例。顺序是对的,但是排名不是我想做的。
Name Employee_ID Order_Number City Rank
John 1 1 Boston 1
John 1 2 Boston 2
Will 2 1 Peabody 1
Will 2 2 Weston 2
Will 2 3 Newton 3
select Name, Employee_ID, Order_Number, City,
dense_rank() over(partition by Employee_ID order by Order_Number) as rank
from #Employee
我实际想要的结果是:
Name Employee_ID Order_Number City Rank
John 1 1 Boston 1
John 1 2 Boston 1
Will 2 1 Boston 1
Will 2 2 Weston 2
Will 2 3 Newton 3
然后我最终会删除重复的城市以结束:
Name Employee_ID Order_Number City Rank
John 1 1 Boston 1
Will 2 1 Boston 1
Will 2 2 Weston 2
Will 2 3 Newton 3
您可以尝试使用以下脚本来获得所需的输出。
SELECT Name, Employee_ID, Order_Number, City ,
ROW_NUMBER() OVER (PARTITION BY Employee_ID ORDER BY Order_Number) rank
(
select Name, Employee_ID, Order_Number, City,
dense_rank() over(partition by Employee_ID,city order by Order_Number) as rank
from #Employee
)A
WHERE rank = 1
结果集的输出是-
Name Employee_ID Order_Number City rank
John 1 1 Boston 1
Will 2 1 Peabody 1
Will 2 2 Weston 2
Will 2 3 Newton 3
检查 Fiddle 上脚本的输出。
您可以使用LAG()
检查之前的城市是否相同。如果前一个城市不同或为空,那么我们按原样进行排名,如果城市相同,那么排名 - 1 给我们的数字与上面的行相同。 Demo
with cte as (select Name, Employee_ID, Order_Number, City,
dense_rank() over (partition by Employee_ID order by Order_Number) as rank,
lag(City) over (partition by Employee_ID order by Order_Number) as previousCity
from #Employee)
select
Name, Employee_ID, Order_Number, City,
case when previousCity = city then rank - 1
else rank end as rank
from cte
我有四列要排名。他们需要按员工 ID 分组,然后按订单号从低到高列出。然后,当一切都井井有条时,我真的在努力获得城市在该顺序中的排名。如果同一个员工在同一个城市中依次列出,那么我希望那些排名相同的城市。
下面是 table 的示例。顺序是对的,但是排名不是我想做的。
Name Employee_ID Order_Number City Rank
John 1 1 Boston 1
John 1 2 Boston 2
Will 2 1 Peabody 1
Will 2 2 Weston 2
Will 2 3 Newton 3
select Name, Employee_ID, Order_Number, City,
dense_rank() over(partition by Employee_ID order by Order_Number) as rank
from #Employee
我实际想要的结果是:
Name Employee_ID Order_Number City Rank
John 1 1 Boston 1
John 1 2 Boston 1
Will 2 1 Boston 1
Will 2 2 Weston 2
Will 2 3 Newton 3
然后我最终会删除重复的城市以结束:
Name Employee_ID Order_Number City Rank
John 1 1 Boston 1
Will 2 1 Boston 1
Will 2 2 Weston 2
Will 2 3 Newton 3
您可以尝试使用以下脚本来获得所需的输出。
SELECT Name, Employee_ID, Order_Number, City ,
ROW_NUMBER() OVER (PARTITION BY Employee_ID ORDER BY Order_Number) rank
(
select Name, Employee_ID, Order_Number, City,
dense_rank() over(partition by Employee_ID,city order by Order_Number) as rank
from #Employee
)A
WHERE rank = 1
结果集的输出是-
Name Employee_ID Order_Number City rank
John 1 1 Boston 1
Will 2 1 Peabody 1
Will 2 2 Weston 2
Will 2 3 Newton 3
检查 Fiddle 上脚本的输出。
您可以使用LAG()
检查之前的城市是否相同。如果前一个城市不同或为空,那么我们按原样进行排名,如果城市相同,那么排名 - 1 给我们的数字与上面的行相同。 Demo
with cte as (select Name, Employee_ID, Order_Number, City,
dense_rank() over (partition by Employee_ID order by Order_Number) as rank,
lag(City) over (partition by Employee_ID order by Order_Number) as previousCity
from #Employee)
select
Name, Employee_ID, Order_Number, City,
case when previousCity = city then rank - 1
else rank end as rank
from cte