我有以下两个表:
- DimensionTime是一个包含每个月的表,按ID排序,格式为YYYYM00
- 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
这是一种间隙和孤岛问题。即使在SQL Server 2005等不受支持的古代软件中也可以解决,因为该版本具有row_number()
。
一个技巧是将时间id转换为真正的日期/时间。另一个技巧是通过从日期/时间值中减去连续的月份来定义组:
select player, team, min(timeid), max(timeid)
from (select lp.*,
row_number() over (partition by player, team order by timeid) as seqnum,
cast(cast(timeid + 1 as varchar(255)) as datetime) as yyyymm
from logplayer lp
) lp
group by player, team, dateadd(month, - seqnum, yyyymm)
order by player, team, min(timeid);
这是一个数据库<>不停摆弄
您可以做这样的事情来查找日期的开始和结束。
通过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 ) ;