SQL查询:如何对row_number()进行排序和显示

huangapple go评论67阅读模式
英文:

SQL Query: how to sort and display row_number()

问题

我尝试计算按两列(User_location和Map_id)分组的行数,但它没有给我想要看到的值。它只允许我按一列分组。

使用的代码:

SELECT ROW_NUMBER() OVER(PARTITION BY User_location, Map_id
    ORDER BY User_location, Map_id ASC) AS Row_Num, 
    Session_id, User_location, Map_id, loc_div 
FROM table

这是我得到的结果。

| Row_num | Session_id | User_location | Map_id | loc_div |
| :--------: | :--------: | :--------------: | :--------------: | :--------------: |
| 1 | XD001| Delta 1| A1| loc010|
| 2 | XD002| Delta 1| A1| loc020|
| 3 | XD003| Delta 1| A1| loc040|
| 1 | XD004| Delta 1| A2| loc050|
| 2 | XD005| Delta 1| A2| loc060|
| 1 | XD006| Delta 2| A1| loc070|
| 2 | XD009| Delta 2| A1| loc030|
| 1 | XD007| Delta 2| A2| loc080|
| 2 | XD008| Delta 2| A2| loc090|

尝试实现按user_location和map_id分组的分开的行号,如下所示:

| Row_num | Session_id | User_location | Map_id | loc_div |
| :--------: | :--------: | :--------------: | :--------------: | :--------------: |
| 1 | XD001| Delta 1| A1| loc010|
| 2 | XD002| Delta 1| A1| loc020|
| 3 | XD003| Delta 1| A1| loc040|
| 1 | XD004| Delta 1| A2| loc050|
| 2 | XD005| Delta 1| A2| loc060|
| 1 | XD006| Delta 2| A1| loc070|
| 2 | XD009| Delta 2| A1| loc030|
| 1 | XD007| Delta 2| A2| loc080|
| 2 | XD008| Delta 2| A2| loc090|
英文:

I am trying to calculate the number of rows grouped by two columns (User_location & Map_id), but it's not giving me the value that I want to see. Its only allowing me to group by one column.

Session_id User_location Map_id loc_div
XD001 Delta 1 A1 loc010
XD002 Delta 1 A1 loc020
XD003 Delta 1 A1 loc040
XD004 Delta 1 A2 loc050
XD005 Delta 1 A2 loc060
XD006 Delta 2 A1 loc070
XD007 Delta 2 A2 loc080
XD008 Delta 2 A2 loc090
XD009 Delta 2 A1 loc030

Used Code:

SELECT ROW_NUMBER() OVER(PARTITION BY User_location
    ORDER BY Map_id ASC) AS Row_Num, 
    Session_id, User_location, Map_id, loc_div 
FROM table

This is what I am getting.

Row_num Session_id User_location Map_id loc_div
1 XD001 Delta 1 A1 loc010
2 XD002 Delta 1 A1 loc020
3 XD003 Delta 1 A1 loc040
4 XD004 Delta 1 A2 loc050
5 XD005 Delta 1 A2 loc060
1 XD006 Delta 2 A1 loc070
2 XD009 Delta 2 A1 loc030
3 XD007 Delta 2 A2 loc080
4 XD008 Delta 2 A2 loc090

Trying to achieve separate row numbers grouped by user_location and map_id like below

Row_num Session_id User_location Map_id loc_div
1 XD001 Delta 1 A1 loc010
2 XD002 Delta 1 A1 loc020
3 XD003 Delta 1 A1 loc040
1 XD004 Delta 1 A2 loc050
2 XD005 Delta 1 A2 loc060
1 XD006 Delta 2 A1 loc070
2 XD009 Delta 2 A1 loc030
1 XD007 Delta 2 A2 loc080
2 XD008 Delta 2 A2 loc090

答案1

得分: 1

你可以将Session_idloc_div中的任一项添加到行号的分区中。假设选择后者,你可以尝试:

SELECT ROW_NUMBER() OVER (PARTITION BY User_location, loc_div
                          ORDER BY Map_id) AS Row_Num, 
       Session_id, User_location, Map_id, loc_div 
FROM yourTable
ORDER BY User_location, Map_id, loc_div;
英文:

You may add either the Session_id or the loc_div to the row number partition. Assuming the latter, you may try:

<!-- language: sql -->

SELECT ROW_NUMBER() OVER (PARTITION BY User_location, loc_div
                          ORDER BY Map_id) AS Row_Num, 
       Session_id, User_location, Map_id, loc_div 
FROM yourTable
ORDER BY User_location, Map_id, loc_div;

huangapple
  • 本文由 发表于 2023年3月23日 11:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定