分组数据排名的最高值
Highest value of the rank for a grouped data
来自下面给出的数据(Table : timeFramesDetail)
tfgroup
City
activeDTTM
Begin_time
End_time
RankOfTime
2
16
2021-04-05 02:30:03.510
2021-04-04 18:00:00.000
2021-04-05 06:00:00.000
1
2
16
2021-04-05 02:30:04.510
2021-04-04 18:00:00.000
2021-04-05 06:00:00.000
2
3
16
2021-04-06 02:30:04.667
2021-04-05 18:00:00.000
2021-04-06 06:00:00.000
1
3
16
2021-04-06 02:30:05.433
2021-04-05 18:00:00.000
2021-04-06 06:00:00.000
2
3
16
2021-04-06 02:30:10.777
2021-04-05 18:00:00.000
2021-04-06 06:00:00.000
3
3
16
2021-04-06 02:30:11.667
2021-04-05 18:00:00.000
2021-04-06 06:00:00.000
4
4
16
2021-04-07 03:27:31.977
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
1
4
16
2021-04-07 03:27:59.523
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
2
4
16
2021-04-07 03:28:30.283
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
3
4
16
2021-04-07 03:28:31.257
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
4
4
16
2021-04-07 03:28:35.617
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
5
4
16
2021-04-07 03:28:54.837
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
6
4
16
2021-04-07 03:29:35.807
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
7
4
16
2021-04-07 03:30:34.793
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
8
如何找到所有tfGroups中价值最高的排名?
结果应该如下:
tfgroup
City
activeDTTM
Begin_time
End_time
RankOfTime
2
16
2021-04-05 02:30:04.510
2021-04-04 18:00:00.000
2021-04-05 06:00:00.000
2
3
16
2021-04-06 02:30:11.667
2021-04-05 18:00:00.000
2021-04-06 06:00:00.000
4
4
16
2021-04-07 03:30:34.793
2021-04-06 18:00:00.000
2021-04-07 06:00:00.000
8
以下是我试过的查询:
Select top 3 tfgroup ,City, activeDTTM
From timeFramesDetail
Group by tfgroup ,City, ActiveDTTM
出于某种原因,这给了我前两个准确的结果,其余的是错误的。
有人可以帮我更正查询吗?
您可以使用 window function
进行分区并 select 所需的排名如下:
select top (1) with ties *
from timeFramesDetail
order by row_number() over (partition by tfgroup order by RankOfTime desc)
来自下面给出的数据(Table : timeFramesDetail)
tfgroup | City | activeDTTM | Begin_time | End_time | RankOfTime |
---|---|---|---|---|---|
2 | 16 | 2021-04-05 02:30:03.510 | 2021-04-04 18:00:00.000 | 2021-04-05 06:00:00.000 | 1 |
2 | 16 | 2021-04-05 02:30:04.510 | 2021-04-04 18:00:00.000 | 2021-04-05 06:00:00.000 | 2 |
3 | 16 | 2021-04-06 02:30:04.667 | 2021-04-05 18:00:00.000 | 2021-04-06 06:00:00.000 | 1 |
3 | 16 | 2021-04-06 02:30:05.433 | 2021-04-05 18:00:00.000 | 2021-04-06 06:00:00.000 | 2 |
3 | 16 | 2021-04-06 02:30:10.777 | 2021-04-05 18:00:00.000 | 2021-04-06 06:00:00.000 | 3 |
3 | 16 | 2021-04-06 02:30:11.667 | 2021-04-05 18:00:00.000 | 2021-04-06 06:00:00.000 | 4 |
4 | 16 | 2021-04-07 03:27:31.977 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 1 |
4 | 16 | 2021-04-07 03:27:59.523 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 2 |
4 | 16 | 2021-04-07 03:28:30.283 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 3 |
4 | 16 | 2021-04-07 03:28:31.257 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 4 |
4 | 16 | 2021-04-07 03:28:35.617 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 5 |
4 | 16 | 2021-04-07 03:28:54.837 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 6 |
4 | 16 | 2021-04-07 03:29:35.807 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 7 |
4 | 16 | 2021-04-07 03:30:34.793 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 8 |
如何找到所有tfGroups中价值最高的排名? 结果应该如下:
tfgroup | City | activeDTTM | Begin_time | End_time | RankOfTime |
---|---|---|---|---|---|
2 | 16 | 2021-04-05 02:30:04.510 | 2021-04-04 18:00:00.000 | 2021-04-05 06:00:00.000 | 2 |
3 | 16 | 2021-04-06 02:30:11.667 | 2021-04-05 18:00:00.000 | 2021-04-06 06:00:00.000 | 4 |
4 | 16 | 2021-04-07 03:30:34.793 | 2021-04-06 18:00:00.000 | 2021-04-07 06:00:00.000 | 8 |
以下是我试过的查询:
Select top 3 tfgroup ,City, activeDTTM
From timeFramesDetail
Group by tfgroup ,City, ActiveDTTM
出于某种原因,这给了我前两个准确的结果,其余的是错误的。 有人可以帮我更正查询吗?
您可以使用 window function
进行分区并 select 所需的排名如下:
select top (1) with ties *
from timeFramesDetail
order by row_number() over (partition by tfgroup order by RankOfTime desc)