SQL:按日期对列进行排名/分组

SQL: Rank / Group a Column by Date

使用 SQL Server Management Studio v17.9.1

我正在尝试按站点和区域按日期对某些数据进行排名/排序/分组,但我正在努力解决问题,而不是按字母顺序对区域进行排名,而是按它出现的最早日期对其进行排名。

这是我的数据:

Site   |   Area   |   Space   |   Date
DCG        X          7           02/02/2020 12:13
DCG        X          5           04/02/2020 11:47
DCG        X          12          10/02/2020 15:14
GNL        U          0           03/03/2020 18:35
GNL        A          4           04/03/2020 08:28
GNL        C          4           06/03/2020 09:07
GNL        B          1           16/03/2020 07:10
DPL        U          0           18/03/2020 09:28
DPL        A          1           18/03/2020 09:36
DPL        A          1           20/03/2020 20:04
SGR        F          2           21/03/2020 19:42
SGR        B          2           22/03/2020 10:30
SGR        C          3           24/03/2020 08:17
SGR        F          1           01/04/2020 09:00
SGR        E          1           02/02/2020 10:57
SGR        F          1           02/02/2020 15:50

我想添加 2 列,按日期的升序对站点和区域进行排名/分组,如下所示:

Site   |   Area   |   Space   |   Date              |   Site Order   |   Area Order   |
DCG        X          7           02/02/2020 12:13         1                 1
DCG        X          5           04/02/2020 11:47         1                 1
DCG        X          12          10/02/2020 15:14         1                 1
GNL        U          0           03/03/2020 18:35         2                 1
GNL        A          4           04/03/2020 08:28         2                 2
GNL        C          4           06/03/2020 09:07         2                 3 
GNL        B          1           16/03/2020 07:10         2                 4
DPL        U          0           18/03/2020 09:28         3                 1
DPL        A          1           18/03/2020 09:36         3                 2 
DPL        A          1           20/03/2020 20:04         3                 2
SGR        F          2           21/03/2020 19:42         4                 1
SGR        B          2           22/03/2020 10:30         4                 2
SGR        C          3           24/03/2020 08:17         4                 3
SGR        F          1           01/04/2020 09:00         4                 1
SGR        E          1           02/02/2020 10:57         4                 4
SGR        F          1           02/02/2020 15:50         4                 1

抱歉,如果我没有说清楚

您可以将 min() 用作 window 函数来获取每个站点和 site/area 组合的最短日期。然后使用 dense_rank():

select t.*,
       dense_rank() over (order by min_site_date, site) as site_seqnum,
       dense_rank() over (partition by site order by min_site_date) as area_seqnum
from (select t.*,
             min(date) over (partition by site) as min_site_date,
             min(date) over (partition by site, area) as min_site_area_date
      from t
     ) t

您可以使用window函数:

select t.*, 
       dense_rank() over (order by site, site_date) as site_sequence,
       dense_rank() over (partition by site order by area, site_area_date) as area_sequence
from (select t.*,
            min([date]) over (partition by [site]) as site_date,
            min([date]) over (partition by [site], area) as site_area_date
      from table t
     ) t;