I have the following two tables:
- DimensionTime is a table that contains every month, ordered by ID, in a YYYMM00 form
- LogPlayer is a table where there are some statistics related to a player and a specific month.
What I'd like to get is the following:
+--------+--------+----------+----------+
| Player | Team | Start | End |
+--------+--------+----------+----------+
| John | Red | 20180100 | 20180300 |
| John | Red | 20180600 | 20180700 |
| Luke | Yellow | 20180100 | 20180100 |
| Luke | Yellow | 20190100 | 20190100 |
+--------+--------+----------+----------+
I can't use MIN and MAX functions because the periods are discontinuous...how can I resolve? I have tryied with MIN/MAX combined with GROUP BY but I get nothing useful. I dind't find any question or answer here on 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
you could do this sort of thing to find the begining and end of runs of dates.
Convert to 'date' via a CTE (which I think exists in 2005) then use Cross Apply an EXIST to find the start and end of a run of dates
you give no data for player and team, but you could add to the WHERE conditions in EXISTS , then GROUP BY - if needed
taking a guess as to some sample data, I tried
with the latest tables that sorry I missed, I devised
This is a type of gaps-and-islands problem. It is solvable even in non-supported ancient software like SQL Server 2005, because that version has
row_number()
.One trick is converting the time id to a bona fide date/time. The other trick is to define the groups by subtracting a sequential number of months from the date/time value:
Here is a db<>fiddle.