在不连续的时间段YYYYMM00中找到MIN和MAX日期

时间:2018-12-11 14:49:10

标签: sql sql-server datetime sql-server-2005

我有以下两个表:

  • DimensionTime 是一个表格,其中包含以YYYMM00格式按ID排序的每个月
  • LogPlayer 是一个表格,其中有一些与玩家和特定月份有关的统计信息。

我想要得到的是以下内容:

+--------+--------+----------+----------+
| Player |  Team  |  Start   |   End    |
+--------+--------+----------+----------+
| John   | Red    | 20180100 | 20180300 |
| John   | Red    | 20180600 | 20180700 |
| Luke   | Yellow | 20180100 | 20180100 |
| Luke   | Yellow | 20190100 | 20190100 |
+--------+--------+----------+----------+

我不能使用MIN和MAX函数,因为周期是不连续的...我该如何解决? 我尝试将MIN / MAX与GROUP BY结合使用,但没有任何用处。我在Stackoverflow上找不到任何问题或答案。

SELECT *
    INTO #DimensionTime
    FROM (
        SELECT 1 AS [ID], 20180100 AS [TIMEID]
        UNION ALL
        SELECT 2 AS [ID], 20180200 AS [TIMEID]
        UNION ALL
        SELECT 3 AS [ID], 20180300 AS [TIMEID]
        UNION ALL
        SELECT 4 AS [ID], 20180400 AS [TIMEID]
        UNION ALL
        SELECT 5 AS [ID], 20180500 AS [TIMEID]
        UNION ALL
        SELECT 6 AS [ID], 20180600 AS [TIMEID]
        UNION ALL
        SELECT 7 AS [ID], 20180700 AS [TIMEID]
        UNION ALL
        SELECT 8 AS [ID], 20180800 AS [TIMEID]
        UNION ALL
        SELECT 9 AS [ID], 20180900 AS [TIMEID]
        UNION ALL
        SELECT 10 AS [ID], 20181000 AS [TIMEID]
        UNION ALL
        SELECT 11 AS [ID], 20181100 AS [TIMEID]
        UNION ALL
        SELECT 12 AS [ID], 20181200 AS [TIMEID]
        UNION ALL
        SELECT 13 AS [ID], 20190100 AS [TIMEID]
        UNION ALL
        SELECT 14 AS [ID], 20190200 AS [TIMEID]
        UNION ALL
        SELECT 15 AS [ID], 20190300 AS [TIMEID]
    ) A

SELECT *
INTO #LogPlayer
FROM (
    SELECT 'John' AS [Player], 'Red' AS [Team], 20180100 AS [TIMEID]
    UNION ALL
    SELECT 'John' AS [Player], 'Red' AS [Team], 20180200 AS [TIMEID]
    UNION ALL
    SELECT 'John' AS [Player], 'Red' AS [Team], 20180300 AS [TIMEID]
    UNION ALL
    SELECT 'John' AS [Player], 'Red' AS [Team], 20180600 AS [TIMEID]
    UNION ALL
    SELECT 'John' AS [Player], 'Red' AS [Team], 20180700 AS [TIMEID]
    UNION ALL
    SELECT 'Luke' AS [Player], 'Yellow' AS [Team], 20180100 AS [TIMEID]
    UNION ALL
    SELECT 'Luke' AS [Player], 'Yellow' AS [Team], 20190100 AS [TIMEID]
) B

2 个答案:

答案 0 :(得分:2)

这是一种空白和岛屿问题。即使在不受支持的古老软件(例如SQL Server 2005)中,它也可以解决,因为该版本具有lst = ['101023', '101011', '102010', '102931'] matrix = [[int(c) for c in seq] for seq in lst] print(matrix) # [[1, 0, 1, 0, 2, 3], [1, 0, 1, 0, 1, 1], [1, 0, 2, 0, 1, 0], [1, 0, 2, 9, 3, 1]]

一个技巧是将时间id转换为善意日期/时间。另一个技巧是通过从日期/时间值中减去连续的月数来定义组:

row_number()

Here是db <>小提琴。

答案 1 :(得分:1)

您可以执行这种操作来查找日期运行的开始和结束。

通过CTE转换为“日期”(我认为该日期在2005年存在) 然后使用交叉应用EXIST查找日期运行的开始和结束

您没有提供球员和球队的数据,但是您可以在EXISTS中添加WHERE条件,然后在需要时添加GROUP BY-

;WITH dats as (SELECT CAST(LEFT(timeid, 6) + '01' as datetime) as DT from #DimensionTime)
    select CONVERT(varchar(7),d1.DT,112) +'0'  as strt, 
           CONVERT(varchar(7),dq.dt,112) +'0' as [end] from dats d1 
        CROSS  APPLY
          (SELECT TOP 1 d3.dt from dats d3 where 
                                            d3.dt >  d1.dt 
                                            and 
                                            not exists(
                                                    select 0 from dats d4 where d4.DT = dateadd(month,1,d3.DT)
                                                      )
        ORDER BY d3.dt asc) DQ           
        where not exists(select 0 from dats d2 where d2.DT = dateadd(month,-1,d1.DT)) ;

我猜了一些示例数据

SELECT *
INTO #DimensionTime
FROM (
    SELECT 1 AS [ID], 20180100 AS [TIMEID], 'john' as player, 'red' as team 
    UNION ALL
    SELECT 2 AS [ID], 20180200 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 3 AS [ID], 20180300 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 4 AS [ID], 20180400 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 5 AS [ID], 20180500 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 7 AS [ID], 20180700 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 8 AS [ID], 20180800 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 9 AS [ID], 20180900 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 11 AS [ID], 20181100 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 12 AS [ID], 20181200 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 13 AS [ID], 20190100 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 14 AS [ID], 20190200 AS [TIMEID], 'john','red'
    UNION ALL
    SELECT 15 AS [ID], 20190300 AS [TIMEID], 'john','red'
    UNION ALL 
    SELECT 1 AS [ID], 20180100 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 2 AS [ID], 20180200 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 4 AS [ID], 20180400 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 5 AS [ID], 20180500 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 8 AS [ID], 20180800 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 9 AS [ID], 20180900 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 12 AS [ID], 20181200 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 13 AS [ID], 20190100 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 14 AS [ID], 20190200 AS [TIMEID], 'luke','yellow'
    UNION ALL
    SELECT 15 AS [ID], 20190300 AS [TIMEID], 'luke','yellow'


) A



;WITH dats as (SELECT CAST(LEFT(timeid, 6) + '01' as datetime) as DT,player,team from #DimensionTime)
    select d1.team,d1.player,
            CONVERT(varchar(7),d1.DT,112) +'0'  as strt, 
           CONVERT(varchar(7),dq.dt,112) +'0' as [end] from dats d1 
        CROSS  APPLY
          (SELECT TOP 1 d3.dt from dats d3 where 
                                            d3.dt >  d1.dt 
                                            and
                                            d3.player = d1.player
                                            and
                                            d3.team = d1.team
                                            and 
                                            not exists(
                                                    select 0 from dats d4 where d4.DT = dateadd(month,1,d3.Dt)
                                                         and  d4.team = d3.team
                                                         and d4.player = d3.player
                                                      )
        ORDER BY d3.dt asc) DQ           
        where not exists(select 0 from dats d2 where 
                    d2.player=d1.player
                    and
                    d2.team = d1.team
                    and 
                    d2.DT = dateadd(month,-1,d1.DT) and d1.team=d2.team and d1.player = d2.player ) ;

drop table #DimensionTime;

我想出了我错过的最新桌子

;WITH dats as (SELECT CAST(LEFT(timeid, 6) + '01' as datetime) as DT,player,team from #LogPlayer)
    select d1.team,d1.player,
            CONVERT(varchar(7),d1.DT,112) +'0'  as strt, 
           CONVERT(varchar(7),dq.dt,112) +'0' as [end] from dats d1 
        CROSS  APPLY
          (SELECT TOP 1 d3.dt from dats d3 where 
                                            d3.dt >  d1.dt 
                                            and
                                            d3.player = d1.player
                                            and
                                            d3.team = d1.team
                                            and 
                                            not exists(
                                                    select 0 from dats d4 where d4.DT = dateadd(month,1,d3.Dt)
                                                         and  d4.team = d3.team
                                                         and d4.player = d3.player
                                                      )
        ORDER BY d3.dt asc) DQ           
        where not exists(select 0 from dats d2 where 
                    d2.player=d1.player
                    and
                    d2.team = d1.team
                    and 
                    d2.DT = dateadd(month,-1,d1.DT) and d1.team=d2.team and d1.player = d2.player ) ;