Related
I was facing this problem and spend a lot of time today. So, i thought to share it here:
I have a table where we store debitDate and we have a stored procedure where every month we set the debit date to next month in the table.
So, if its debit date is 29th Jan, 2020 -> 29th Feb, 2020 -> 29th March, 2020 - so it should go on like this. I am using DATEADD() function in the stored procedure.
But for 30th & 31st i am facing issue. It should work like below in upcoming years:
Desired Behaviour:
30th Jan, 2020 -> 29th Feb, 2020 -> 30th Mar, 2020 -> 30th Apr, 2020
30th Jan, 2021 -> 28th Feb, 2021 -> 30th Mar, 2021 -> 30th Apr, 2021
31st Jan, 2020 -> 29th Feb, 2020 -> 31st Mar, 2020 -> 30th Apr, 2020
Issue:
30th Jan, 2020 -> 29th Feb, 2020 -> 29th Mar, 2020 -> 29th Apr, 2020
30th Jan, 2021 -> 28th Feb, 2021 -> 28th Mar, 2021 -> 28th Apr, 2021
31st Jan, 2020 -> 29th Feb, 2020 -> 29th Mar, 2020 -> 29th Apr, 2020
Solution 1:
For solution i have thought i can add a new column to the table as previousDebitDate and when we update the debit date we will check, if previousDebitDate day is 30 or 31.
If true then
DATEADD(MONTH, 2, #previousDebitDate)
else
DATEADD(MONTH, 1, #debitDate)
If anyone has a better solution please feel free to post your answer.
Solution 2:
For this issue a better solution is to add debitDay as a new column to the table and save only day part (ex: 30) and calculate each month debit date on the fly.
I think Solution 2 is better! Thanks #Arvo!!!
Maybe I 've understand very well & maybe not, but here's what I think you're looking for
CREATE TABLE Data
(
Dates DATE
);
INSERT Data(Dates) VALUES
('2020-01-30');
WITH CTE AS
(
SELECT Dates,
DATEADD(Month, 1, Dates) NextMonth,
DAY(EOMONTH(DATEADD(Month, 1, Dates))) LastDay
FROM Data
UNION ALL
SELECT DATEADD(Month, 1, Dates),
DATEADD(Month, 1, NextMonth),
DAY(EOMONTH(DATEADD(Month, 1, NextMonth)))
FROM CTE
WHERE Dates <= '2021-12-31'
)
SELECT Dates, NextMonth, DATEFROMPARTS(YEAR(Dates), MONTH(NextMonth),
CASE WHEN LastDay > 30 THEN 30 ELSE LastDay END) Value
FROM CTE;
Which 'll returns:
+------------+------------+------------+
| Dates | NextMonth | Value |
+------------+------------+------------+
| 2020-01-30 | 2020-02-29 | 2020-02-29 |
| 2020-02-29 | 2020-03-29 | 2020-03-30 |
| 2020-03-29 | 2020-04-29 | 2020-04-30 |
| 2020-04-29 | 2020-05-29 | 2020-05-30 |
| 2020-05-29 | 2020-06-29 | 2020-06-30 |
| 2020-06-29 | 2020-07-29 | 2020-07-30 |
| 2020-07-29 | 2020-08-29 | 2020-08-30 |
| 2020-08-29 | 2020-09-29 | 2020-09-30 |
| 2020-09-29 | 2020-10-29 | 2020-10-30 |
| 2020-10-29 | 2020-11-29 | 2020-11-30 |
| 2020-11-29 | 2020-12-29 | 2020-12-30 |
| 2020-12-29 | 2021-01-29 | 2020-01-30 |
| 2021-01-29 | 2021-02-28 | 2021-02-28 |
| 2021-02-28 | 2021-03-28 | 2021-03-30 |
| 2021-03-28 | 2021-04-28 | 2021-04-30 |
| 2021-04-28 | 2021-05-28 | 2021-05-30 |
| 2021-05-28 | 2021-06-28 | 2021-06-30 |
| 2021-06-28 | 2021-07-28 | 2021-07-30 |
| 2021-07-28 | 2021-08-28 | 2021-08-30 |
| 2021-08-28 | 2021-09-28 | 2021-09-30 |
| 2021-09-28 | 2021-10-28 | 2021-10-30 |
| 2021-10-28 | 2021-11-28 | 2021-11-30 |
| 2021-11-28 | 2021-12-28 | 2021-12-30 |
| 2021-12-28 | 2022-01-28 | 2021-01-30 |
| 2022-01-28 | 2022-02-28 | 2022-02-28 |
+------------+------------+------------+
Much better
WITH CTE AS
(
SELECT 1 N, Dates, Dates ExpectedValue
FROM Data
UNION ALL
SELECT N+1, DATEADD(Month, 1, Dates), DATEFROMPARTS(YEAR(ExpectedValue), MONTH(DATEADD(Month, 1, ExpectedValue)),
CASE WHEN DAY(EOMONTH(DATEADD(Month, 1, ExpectedValue))) > 30 THEN 30
ELSE DAY(EOMONTH(DATEADD(Month, 1, ExpectedValue)))
END)
FROM CTE
WHERE N < 15
)
SELECT *
FROM CTE
ORDER BY N;
Returns:
+----+------------+---------------+
| N | Dates | ExpectedValue |
+----+------------+---------------+
| 1 | 2020-01-30 | 2020-01-30 |
| 2 | 2020-02-29 | 2020-02-29 |
| 3 | 2020-03-29 | 2020-03-30 |
| 4 | 2020-04-29 | 2020-04-30 |
| 5 | 2020-05-29 | 2020-05-30 |
| 6 | 2020-06-29 | 2020-06-30 |
| 7 | 2020-07-29 | 2020-07-30 |
| 8 | 2020-08-29 | 2020-08-30 |
| 9 | 2020-09-29 | 2020-09-30 |
| 10 | 2020-10-29 | 2020-10-30 |
| 11 | 2020-11-29 | 2020-11-30 |
| 12 | 2020-12-29 | 2020-12-30 |
| 13 | 2021-01-29 | 2020-01-30 |
| 14 | 2021-02-28 | 2020-02-29 |
| 15 | 2021-03-28 | 2020-03-30 |
+----+------------+---------------+
Here is a db<>fiddle
I need to fill up columns by number of day in quarters. Time period is about 10 years. Structure of tables is below. Thanks for any suggestions
SELECT StartDate, EndDate
INTO #tmp_RTX
FROM DateTable
StartDate EndDate
-------------------------------------------------
2015-11-01 00:00:00.000 2018-06-01 00:00:00.000
2017-09-02 00:00:00.000 2021-12-02 00:00:00.000
2016-01-02 00:00:00.000 2019-01-02 00:00:00.000
.
.
.
2018-10-26 00:00:00.000 2020-10-26 00:00:00.000
INSERT INTO DWHMart.[RTX].[RoadTax]
( ,[NuberOfaDaysInQuarter1]
,[NuberOfaDaysInQuarter2]
,[NuberOfaDaysInQuarter3]
,[NuberOfaDaysInOctoberNovember]
,[NuberOfaDaysInDecember]
,[NuberOfaDaysInTotal])
SELECT
,[NuberOfaDaysInQuarter1] = NuberOfaDaysInQuarter1
,[NuberOfaDaysInQuarter2] = NuberOfaDaysInQuarter2
,[NuberOfaDaysInQuarter3] = NuberOfaDaysInQuarter3
,[NuberOfaDaysInOctoberNovember] = NuberOfaDaysInOctoberNovember
,[NuberOfaDaysInDecember] = NuberOfaDaysInDecember
,[NuberOfaDaysInTotal] = DATEDIFF(DAY, StartDate, EndDate)
FROM #tmp_RTX
EXPECTED RESULT:
StartDate EndDate NuberOfaDaysInQuarter1 ..
2015-11-01 00:00:00.000 2018-06-01 00:00:00.000 .
2017-09-02 00:00:00.000 2021-12-02 00:00:00.000 .
2016-01-02 00:00:00.000 2019-01-02 00:00:00.000 .
. .
. .
. .
2018-10-26 00:00:00.000 2020-10-26 00:00:00.000 .
I've made my own dynamic table of days using a recursive CTE.
Then I've counted by quarter the number of days from it in each range you've given
I used a CROSS JOIN for each of your rows, as they have no unique ID for a GROUP BY, it works in any case though
;WITH RNG AS (SELECT cast(MIN(StartDate) as date) as MN,
cast(MAX (EndDate ) as date) MX FROM #tmp_RTX),
DATS AS (SELECT MN FROM RNG
UNION ALL
SELECT DATEADD(day,1,MN) FROM dats
WHERE dats.MN < (SELECT MX FROM RNG)
)
SELECT StartDate T1,
Enddate T1,
DQ.NuberOfaDaysInQuarter1 ,
DQ.NuberOfaDaysInQuarter2 ,
DQ.NuberOfaDaysInQuarter3 ,
DQ.NuberOfaDaysInQuarter4
FROM #tmp_RTX T1
CROSS APPLY (SELECT
SUM(CASE WHEN datepart(quarter ,MN) = 1 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter1,
SUM(CASE WHEN datepart(quarter ,MN) = 2 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter2,
SUM(CASE WHEN datepart(quarter ,MN) = 3 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter3,
SUM(CASE WHEN datepart(quarter ,MN) = 4 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter4
FROM DATS
WHERE MN BETWEEN
CAST(t1.startdate as date)
and
CAST(T1.EndDate as date)
) DQ
OPTION (maxrecursion 0);
The following was what built my test data (based on the conventions of your data)
SELECT '20151101' StartDate, '20180601' EndDate
INTO #tmp_RTX
INSERT into #tmp_RTX values ('20161231','20180101');
INSERT into #tmp_RTX values ('20181231','20190101');
To handle the different requirement for the final quarter, check the month, not the quarter
;WITH RNG AS (SELECT cast(MIN(StartDate) as date) as MN,
cast(MAX (EndDate ) as date) MX FROM #tmp_RTX),
DATS AS (SELECT MN FROM RNG
UNION ALL
SELECT DATEADD(day,1,MN) FROM dats
WHERE dats.MN < (SELECT MX FROM RNG)
)
SELECT StartDate T1,
Enddate T1,
DQ.NuberOfaDaysInQuarter1 ,
DQ.NuberOfaDaysInQuarter2 ,
DQ.NuberOfaDaysInQuarter3 ,
DQ.NuberOfaDaysInOctoberNovember ,
DQ.NuberOfaDaysInDecember
FROM #tmp_RTX T1
CROSS APPLY (SELECT
SUM(CASE WHEN datepart(quarter ,MN) = 1 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter1,
SUM(CASE WHEN datepart(quarter ,MN) = 2 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter2,
SUM(CASE WHEN datepart(quarter ,MN) = 3 THEN 1 ELSE 0 END)
NuberOfaDaysInQuarter3,
SUM(CASE WHEN datepart(month ,MN) IN(10,11) THEN 1 ELSE 0 END)
NuberOfaDaysInOctoberNovember,
SUM(CASE WHEN datepart(month ,MN) IN(12) THEN 1 ELSE 0 END)
NuberOfaDaysInDecember
FROM DATS
WHERE MN BETWEEN
CAST(t1.startdate as date)
and
CAST(T1.EndDate as date)
) DQ
OPTION (maxrecursion 0)
The number of days in quarters is always the same excluding the leap years. Hence you can use the constant expressions for Q2..Q4 and the parameterized value for Q1.
DECLARE #my_date date = '2017-01-01';
SELECT 89 + ISDATE(CAST(#my_date AS char(4)) + '0229') AS NuberOfaDaysInQuarter1,
90 AS NuberOfaDaysInQuarter2,
91 AS NuberOfaDaysInQuarter3,
91 AS NuberOfaDaysInQuarter4
Updated solution (accumulating day count by quarters)
DECLARE #start_date date = '2000-01-01';
DECLARE #quarters TABLE (start_date date, end_date date, num int);
-- Create quarter calendar from #start_date
WITH nums (n) AS
(
SELECT TOP 100 (ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1) * 3
FROM sys.columns sc1 CROSS JOIN sys.columns sc2
),
calendar AS
(
SELECT n, DATEADD(month, n, #start_date) AS start_date
FROM nums
)
INSERT INTO #quarters
SELECT start_date,
DATEADD(day, -1, DATEADD(month, 3, start_date)) AS end_date,
((n / 3) % 4) + 1 AS num
FROM calendar;
-- test data
DECLARE #t TABLE (start_date date, end_date date);
INSERT INTO #t VALUES
('2015-11-01', '2018-06-01'),
('2017-09-02', '2021-12-02'),
('2016-01-02', '2019-01-02');
-- Calculations
WITH inner_counts AS (
SELECT t.start_date, t.end_date, q.num,
SUM(datediff(day, q.start_date, q.end_date)) AS days_count
FROM #t t INNER JOIN #quarters q ON q.start_date > t.start_date AND q.start_date < t.end_date
GROUP BY t.start_date, t.end_date, q.num
),
outer_counts1 AS (
SELECT t.start_date, t.end_date, q.num,
SUM(datediff(day, t.start_date, q.end_date)) AS days_count
FROM #t t INNER JOIN #quarters q ON q.end_date = (SELECT MIN(end_date) FROM #quarters q WHERE q.end_date >= t.start_date)
GROUP BY t.start_date, t.end_date, q.num
),
outer_counts2 AS (
SELECT t.start_date, t.end_date, q.num,
SUM(datediff(day, q.start_date, t.end_date)) AS days_count
FROM #t t INNER JOIN #quarters q ON q.start_date = (SELECT MAX(start_date) FROM #quarters q WHERE q.start_date <= t.end_date)
GROUP BY t.start_date, t.end_date, q.num
),
total_counts AS (
SELECT start_date, end_date, num, SUM(days_count) AS days_count
FROM (SELECT * FROM inner_counts
UNION ALL
SELECT * FROM outer_counts1
UNION ALL
SELECT * FROM outer_counts2) c
GROUP BY start_date, end_date, num
)
SELECT start_date, end_date,
SUM(CASE WHEN num = 1 THEN days_count ELSE 0 END) AS days_in_Q1,
SUM(CASE WHEN num = 2 THEN days_count ELSE 0 END) AS days_in_Q2,
SUM(CASE WHEN num = 3 THEN days_count ELSE 0 END) AS days_in_Q3,
SUM(CASE WHEN num = 4 THEN days_count ELSE 0 END) AS days_in_Q4
FROM total_counts
GROUP BY start_date, end_date
Result
start_date end_date days_in_Q1 days_in_Q2 days_in_Q3 days_in_Q4
---------- ---------- ----------- ----------- ----------- -----------
2015-11-01 2018-06-01 268 331 182 242
2016-01-02 2019-01-02 357 270 273 273
2017-09-02 2021-12-02 357 360 392 517
DEMO
Use a recursive CTE (Be sure to set max recursion option to the # of days you need the loop to process or 0 once you've confirmed no infinite loops exist. ) to generate all your days between max and min of your dates in datetable and then join this result to your datetable on date between range. using datepart on year and quarter of a cycledate we can get the appropriate quarter/year and then group.
I didn't pivot the results. Pivoting data is usually a display function and best left to those display tools crystal, BI, etc. It can be done in SQL and in in this case Dynamic SQL would be needed as the # of years is dynamic in nature. I choose not to to down that path as the Reporting tools can handle the dynamic nature of the data.
WITH
DateTable as (SELECT cast('2015-11-01 00:00:00.000'as date) startdate, cast('2018-06-01 00:00:00.000'as date) endDate union all
SELECT '2017-09-02 00:00:00.000', '2021-12-02 00:00:00.000' union all
SELECT '2016-01-02 00:00:00.000', '2019-01-02 00:00:00.000'),
CTE AS (SELECT Min(StartDate) StartDate, max(EndDate) EndDate FROM DATETABLE),
CTE2 AS (SELECT C.StartDate as RangeStartDate
, C.EndDate as RangeEndDate
, 1 CycleCount
, datepart(Q,dateadd(d,0,C.StartDate)) as Quarter
, datepart(m,dateadd(d,0,C.StartDate)) as Month
, datepart(YYYY, dateadd(d,0,C.StartDate)) as Yr
, C.StartDate as CycleDate
FROM CTE C
UNION ALL
SELECT RangeStartDate, RangeEndDate, CycleCount+1
, datepart(Q,dateadd(d,1,CycleDate)) as Quarter
, datepart(m,dateadd(d,1,CycleDate)) as Month
, datepart(YYYY, dateadd(d,1,CycleDate)) as Yr
, dateadd(d,1,cycleDate) as CycleDate
FROM cte2
WHERE datediff(d,RangeStartDate, RangeEndDate) >= CycleCount
)
SELECT DT.StartDate
, DT.ENDDate
, concat(yr,'-',Quarter) [YYYY-Q]
, count(*) as DaysInQuarter
, sum(case when Month in (10,11) then 1 else 0 end)as OctNovDays
, sum(case when Month in (12) then 1 else 0 end)as DecDays
, datediff(d,StartDate, EndDate) as DaysTotal
FROM CTE2
INNER JOIN DateTable DT
on CTE2.CycleDate between DT.StartDate and DT.EndDate
GROUP BY Quarter,DT.StartDate, DT.EndDate, YR
ORDER BY DT.StartDate, DT.EndDate, YR, Quarter
OPTION (MAXRECURSION 10000)
Giving us:
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+
| | StartDate | ENDDate | YYYY-Q | DaysInQuarter | OctNovDays | DecDays | DaysTotal |
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+
| 1 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2015-4 | 61 | 30 | 31 | 943 |
| 2 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-1 | 91 | 0 | 0 | 943 |
| 3 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-2 | 91 | 0 | 0 | 943 |
| 4 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-3 | 92 | 0 | 0 | 943 |
| 5 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2016-4 | 92 | 61 | 31 | 943 |
| 6 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-1 | 90 | 0 | 0 | 943 |
| 7 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-2 | 91 | 0 | 0 | 943 |
| 8 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-3 | 92 | 0 | 0 | 943 |
| 9 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2017-4 | 92 | 61 | 31 | 943 |
| 10 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2018-1 | 90 | 0 | 0 | 943 |
| 11 | 01.11.2015 00:00:00 | 01.06.2018 00:00:00 | 2018-2 | 62 | 0 | 0 | 943 |
| 12 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-1 | 90 | 0 | 0 | 1096 |
| 13 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-2 | 91 | 0 | 0 | 1096 |
| 14 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-3 | 92 | 0 | 0 | 1096 |
| 15 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2016-4 | 92 | 61 | 31 | 1096 |
| 16 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-1 | 90 | 0 | 0 | 1096 |
| 17 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-2 | 91 | 0 | 0 | 1096 |
| 18 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-3 | 92 | 0 | 0 | 1096 |
| 19 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2017-4 | 92 | 61 | 31 | 1096 |
| 20 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-1 | 90 | 0 | 0 | 1096 |
| 21 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-2 | 91 | 0 | 0 | 1096 |
| 22 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-3 | 92 | 0 | 0 | 1096 |
| 23 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2018-4 | 92 | 61 | 31 | 1096 |
| 24 | 02.01.2016 00:00:00 | 02.01.2019 00:00:00 | 2019-1 | 2 | 0 | 0 | 1096 |
| 25 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2017-3 | 29 | 0 | 0 | 1552 |
| 26 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2017-4 | 92 | 61 | 31 | 1552 |
| 27 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-1 | 90 | 0 | 0 | 1552 |
| 28 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-2 | 91 | 0 | 0 | 1552 |
| 29 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-3 | 92 | 0 | 0 | 1552 |
| 30 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2018-4 | 92 | 61 | 31 | 1552 |
| 31 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-1 | 90 | 0 | 0 | 1552 |
| 32 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-2 | 91 | 0 | 0 | 1552 |
| 33 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-3 | 92 | 0 | 0 | 1552 |
| 34 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2019-4 | 92 | 61 | 31 | 1552 |
| 35 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-1 | 91 | 0 | 0 | 1552 |
| 36 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-2 | 91 | 0 | 0 | 1552 |
| 37 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-3 | 92 | 0 | 0 | 1552 |
| 38 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2020-4 | 92 | 61 | 31 | 1552 |
| 39 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-1 | 90 | 0 | 0 | 1552 |
| 40 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-2 | 91 | 0 | 0 | 1552 |
| 41 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-3 | 92 | 0 | 0 | 1552 |
| 42 | 02.09.2017 00:00:00 | 02.12.2021 00:00:00 | 2021-4 | 63 | 61 | 2 | 1552 |
+----+---------------------+---------------------+--------+---------------+------------+---------+-----------+
I have a table to track the Student details and there is another table to track the performance of the student.
+==========================================+
| ID | Department | Date |
+==========================================+
| 001 | English | Jan 3 2017 |
| 001 | English | Feb 24 2017 |
| 001 | Science | Mar 1 2017 |
| 001 | Maths | Mar 2 2017 |
| 001 | Maths | Mar 21 2017 |
| 001 | Maths | Apr 2 2017 |
| 001 | English | Apr 7 2017 |
| 002 | Maths | Feb 1 2017 |
| 002 | Maths | Apr 7 2017 |
| 003 | Maths | Apr 3 2017 |
| 003 | Maths | Apr 7 2017 |
| 004 | Science | Feb 1 2017 |
| 004 | Science | Mar 1 2017 |
| 004 | Maths | Apr 7 2017 |
| 004 | English | Apr 9 2017 |
+==========================================+
Performance table:
+===========================================================================+
| ID | Department | Best score| Avg score | Date |
+===========================================================================+
| 001 | English | 98 | 85 | Jan 30 2017 |
| 001 | English | 89 | 80.2 | Apr 14 2017 |
| 001 | Science | 75 | 79.8 | May 1 2017 |
| 001 | Maths | 88 | 80.2 | Jan 12 2017 |
| 001 | Maths | 79 | 75.6 | Feb 21 2017 |
| 001 | Maths | 90 | 80.5 | Jan 20 2017 |
| 001 | English | 80 | 79.3 | Mar 27 2017 |
| 002 | Maths | 90 | 78.4 | Mar 31 2017 |
| 002 | Maths | 85 | 80.2 | May 7 2017 |
| 003 | Maths | 75 | 79.1 | Apr 30 2017 |
| 003 | Maths | 80 | 80.0 | Feb 7 2017 |
| 004 | Science | 60 | 70.3 | May 1 2017 |
| 004 | Science | 72 | 69.9 | Mar 10 2017 |
| 004 | Maths | 70 | 66.8 | Jan 17 2017 |
| 004 | English | 65 | 65.0 | Mar 29 2017 |
+===========================================================================+
I want to get the most recent performance and average score of the student whenever a department change happens in the student table. Considering student 001, the student's dept changes are
| 001 | English | Jan 3 2017 |
| 001 | Science | Mar 1 2017 |
| 001 | Maths | Apr 2 2017 |
For,
Jan 3 2017, There is no date that is less than the date in the Performance table.
Mar 1 2017, The most recent record in performance table is of date Feb 21 2017
Apr 2 2017, The most recent record in performance table is of date Mar 27 2017
Please help me in doing it.
Take a little pain to explain clearly if my script is wrong
Hope the output that you have explain is correct.Becasue i hv lil doubts about the output.
Most importantly hope you have posted exactly same table structure .
Because of your table structure,lil more window function is use than expected,which may hamper your performance.
It is all together very different and important discussion about your real requirement and what should be table structure and how it should be populated.
Try this script with various sample data and let me know,
declare #StudentDetails table(ID varchar(20)
,Department varchar(20),Dates Date)
insert into #StudentDetails VALUES
('001','English','Jan 3 2017 ')
,('001','English','Feb 24 2017')
,('001','Science','Mar 1 2017 ')
,('001','Maths','Mar 2 2017 ')
,('001','Maths','Mar 21 2017')
,('001','Maths','Apr 2 2017 ')
,('001','English','Apr 7 2017 ')
,('002','Maths','Feb 1 2017 ')
,('002','Maths','Apr 7 2017 ')
,('003','Maths','Apr 3 2017 ')
,('003','Maths','Apr 7 2017 ')
,('004','Science','Feb 1 2017 ')
,('004','Science','Mar 1 2017 ')
,('004','Maths','Apr 7 2017 ')
,('004','English','Apr 9 2017 ')
--select * from #StudentDetails
declare #Performance table( ID varchar(20)
,Department varchar(20),Bestscore float,Avgscore float,PDate date)
insert into #Performance VALUES
('001','English',98,85 ,'Jan 30 2017')
,('001','English',89,80.2 ,'Apr 14 2017')
,('001','Science',75,79.8 ,'May 1 2017 ')
,('001','Maths',88,80.2 ,'Jan 12 2017')
,('001','Maths',79,75.6 ,'Feb 21 2017')
,('001','Maths',90,80.5 ,'Jan 20 2017')
,('001','English',80,79.3 ,'Mar 27 2017')
,('002','Maths',90,78.4 ,'Mar 31 2017')
,('002','Maths',85,80.2 ,'May 7 2017 ')
,('003','Maths',75,79.1 ,'Apr 30 2017')
,('003','Maths',80,80.0 ,'Feb 7 2017 ')
,('004','Science',60,70.3 ,'May 1 2017 ')
,('004','Science',72,69.9 ,'Mar 10 2017')
,('004','Maths',70,66.8 ,'Jan 17 2017')
,('004','English',65,65.0 ,'Mar 29 2017')
--select * from #Performance
--declare #SID varchar(20)='001'
;with CTE as
(
select *
,ROW_NUMBER()over(partition by id order by Dates,Department) rn
from #StudentDetails
--where id=#SID
)
,CTE3 AS(
select c.id, c.Department,c.dates,c.rn,1 rn3
from cte c
where rn=1
union ALL
select c.id, c.Department,c.dates,c.rn
,case when c.Department=c3.Department and c.dates>c3.dates
then cast(c3.rn3 as int)
else cast(c3.rn3+1 as int) end
from cte c
inner join cte3 c3
on c.id=c3.id
where c.rn=c3.rn+1
and c.rn<=7
)
,cte4 AS(
select *,0 rn1 from cte3 where rn=1
union ALL
select * from
(
select * ,ROW_NUMBER()over(PARTITION by id,rn3 order by dates desc) rn1
from cte3
where rn3>1
)t4 where t4.rn1=1
)
select c.id,c.department,c.dates,fn.Avgscore AVGScroe,fn.pdate RecentPDate
from cte4 c
OUTER apply(
select * from
(select p.*,ROW_NUMBER()over( order by pdate desc)rn2
from #Performance P
where c.id=p.id and p.pDate<c.dates)t4
where rn2=1 )fn
order by c.id
Could you try this...
SELECT S.ID,S.DATE,MAX(P.AVG_SCORE),MAX(P.BEST_SCORE)
FROM STUDENT S, PERFORMANCE P
WHERE P.DATE < S.DATE AND P.ID = S.ID
GROUP BY S.ID,S.DATE
Thanks..
I need to get start and end date for each week in given month/year. (month and year are always given - like march 2017).
Example, january 2017:
1 week : '2017-01-01' - '2017-01-01'
2 week: '2017-01-02' - '2017-01-08'
3 week: '2017-01-09' - '2017-01-15'
4 week: '2017-01-16' - '2017-01-22'
5 week: '2017-01-23' - '2017-01-29'
6 week: '2017-01-30' - '2017-01-31'
I already know how to get number of weeks for given month / year:
select *,
DATEDIFF(WEEK, DATEADD(day,-1,StartAt), DATEADD(day,-1,EndAt)) +1
as NumWeeks
But how to get start / end date for each week for the given month/year?
If you just want a function to return the weeks of a month for one month then this would do what you want:
create function dbo.udf_weeks_of_month (#fromdate date)
returns table as return (
with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, dates as (
select top (datediff(day, #fromdate, dateadd(month, datediff(month, 0, #fromdate )+1, 0)))
[DateValue]=convert(date,dateadd(day,row_number() over(order by (select 1))-1,#fromdate))
from n as deka cross join n as hecto
)
select
WeekOfMonth = row_number() over (order by datepart(week,DateValue))
, Week = datepart(week,DateValue)
, WeekStart = min(DateValue)
, WeekEnd = max(DateValue)
from dates
group by datepart(week,DateValue)
);
and calling it like so:
set datefirst 1;
select * from dbo.udf_weeks_of_month('20170101');
returns:
+-------------+------+------------+------------+
| WeekOfMonth | Week | WeekStart | WeekEnd |
+-------------+------+------------+------------+
| 1 | 1 | 2017-01-01 | 2017-01-01 |
| 2 | 2 | 2017-01-02 | 2017-01-08 |
| 3 | 3 | 2017-01-09 | 2017-01-15 |
| 4 | 4 | 2017-01-16 | 2017-01-22 |
| 5 | 5 | 2017-01-23 | 2017-01-29 |
| 6 | 6 | 2017-01-30 | 2017-01-31 |
+-------------+------+------------+------------+
and this call:
select * from dbo.udf_weeks_of_month('february 2017');
returns:
+-------------+------+------------+------------+
| WeekOfMonth | Week | WeekStart | WeekEnd |
+-------------+------+------------+------------+
| 1 | 6 | 2017-02-01 | 2017-02-05 |
| 2 | 7 | 2017-02-06 | 2017-02-12 |
| 3 | 8 | 2017-02-13 | 2017-02-19 |
| 4 | 9 | 2017-02-20 | 2017-02-26 |
| 5 | 10 | 2017-02-27 | 2017-02-28 |
+-------------+------+------------+------------+
rextester demo: http://rextester.com/VKPQU7936 (note: rextester reformats the dates)
Something like this?
DECLARE #Dates TABLE (DateId INT IDENTITY, Dt Date);
DECLARE #STart Date = DATEADD(Year, DATEDIFF(Year, 0, GETDATE()),0)
SET NOCOUNT ON;
WHILE #STart <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
BEGIN
INSERT INTO #Dates (Dt) VALUES (#STart)
SELECT #Start = DATEADD(DAY, 1, #STart)
END
Updated
SELECT
DateId
, Dt
, DATEADD(WEEK, DATEDIFF(DAY, 0, Dt)/7, 0) AS WeekBeginningMondayOf
, DATEADD(DAY, 6, DATEADD(WEEK, DATEDIFF(DAY, 0, Dt)/7, 0)) AS WeekEndingSundayOf
, DENSE_RANK() OVER(PARTITION BY MONTH(Dt) ORDER BY DATEADD(WEEK, DATEDIFF(DAY, 0, Dt)/7, 0)) AS WeekInMonth
FROM #Dates
It is very smart in such situations to use a numbers / date / tally table. Such a table is very handsome in many situations!
In this answer I show an approach how to create and fill such a table.
Once you have created this table, the query is as simple as:
SELECT n.CalendarDate AS StartOfWeek
,DATEADD(DAY,6,n.CalendarDate) AS EndOfWeek
FROM dbo.RunningNumbers AS n
WHERE n.CalendarDate>={d'2017-01-01'} AND n.CalendarDate<{d'2017-02-01'}
AND n.CalendarWeekDay=1;
hint Such a table can easily be extended with information hardly to compute simply by adding some values into extra columns manually (e.g. holydays, free-of-work days...)
So many variations on this theme. This one is a little shorter than other answers though.
declare #dt date = {fn current_date()};
declare #start_of_year date = datefromparts(year(#dt), 1, 1);
with digits(d) as (
select 0 union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7
), wks as (
select
dateadd(week, d1.d * 8 + d0.d, dateadd(day, 1-datepart(weekday, #start_of_year), #start_of_year)) as week_start,
d1.d * 8 + d0.d as wk
from digits as d0 cross join digits as d1
)
select
case when year(week_start) < year(#dt)
then #start_of_year else week_start end as week_start,
case when year(dateadd(day, 6, week_start)) > year(#dt)
then datefromparts(year(#dt), 12, 31) else dateadd(day, 6, week_start) end as week_end
from wks
where wk between 0 and 53 and year(week_start) = year(#dt)
order by week_start;
I think what you are looking for is here ->someone asked on stackoverflow is similar to what you are looking for it.
This is the final version, hope as per your expectations
I build on the above link hope this is what you are looking for ( Apologies new to stackoverflow, unformatted t-sql below)
DECLARE #sDate DATETIME,
#eDate DATETIME
SET #sDate = '2017-03-01'
SET #eDate = DATEADD(DAY,-1, CAST(Cast(DatePart(YEAR,#sdate) AS varchar(4))
+'-' + Cast((DatePart(MONTH,#sdate)+1) AS varchar(2))+ '-1' AS Date))
SET #sDate = CAST(Cast(DatePart(YEAR,#sdate) AS varchar(4)) +'-'
+ Cast(DatePart(MONTH,#sdate) AS varchar(2))+ '-1' AS Date)
DECLARE #startDayOfWeekOffSet int ;
SET #startDayOfWeekOffSet=
(SELECT DATEPART(DW,#sDate));
DECLARE #DaysToGetFirstSaturday int
SET #DaysToGetFirstSaturday= 7- #startDayOfWeekOffSet
DECLARE #firstEverStartOfWeekDay Date , #firstWeekEndDay Date
SET #firstEverStartOfWeekDay =#sDate;
IF #startDayOfWeekOffSet = 1 -- January
BEGIN
SET #firstEverStartOfWeekDay = #sDate
SET #DaysToGetFirstSaturday = 0
SET #firstWeekEndDay = #sDate END
ELSE BEGIN IF DATEPART(MONTH,#sDate) > DATEPART(MONTH,#firstEverStartOfWeekDay)
AND #startDayOfWeekOffSet = 7 --FirstSundayNewMonth
BEGIN
SET #firstEverStartOfWeekDay =#sDate
SET #firstWeekEndDay = #sDate END ELSE --NotASundayNewMonth
BEGIN
SET #firstWeekEndDay = DATEADD(DAY,#DaysToGetFirstSaturday,#firstEverStartOfWeekDay);
END END
SELECT #firstWeekEndDay AS firstWeekEndDay,
#firstEverStartOfWeekDay AS firstEverStartOfWeekDay,
#DaysToGetFirstSaturday AS DaysToGetFirstSaturday,
#startDayOfWeekOffSet AS startDayOfWeekOffSet ;
WITH cte AS
(SELECT 1 AS WeekNum,
#firstEverStartOfWeekDay StartDate,
#firstWeekEndDay EndDate
UNION ALL SELECT WeekNum + 1, --Case when #scenario = 'NewYear' Then
dateadd(DAY, 1, cte.EndDate) --Else dateadd(ww, 1, cte.StartDate) End as
StartDate,
CASE
WHEN StartDate = EndDate THEN DateAdd(DAY,-1,dateadd(DAY, 8, cte.EndDate))
ELSE dateadd(ww, 1, cte.EndDate)
END AS EndDate
FROM cte
WHERE dateadd(ww, 1, StartDate)<= #eDate )
SELECT WeekNum,
CASE
WHEN DatePart(YEAR, StartDate) < DATEPART(YEAR,#sDate)
THEN CAST(Cast(DatePart(YEAR,#sdate) AS varchar(4)) + '-1-1' AS Date)
WHEN DatePart(MONTH, StartDate) < DATEPART(MONTH,#sDate)
THEN CAST(Cast(DatePart(YEAR,#sdate) AS varchar(4)) + '-'
+ Cast(DatePart(MONTH,#sdate) AS varchar(2)) + '-1' AS Date)
ELSE StartDate
END AS StartDate,
CASE
WHEN DatePart(MONTH, EndDate) > DATEPART(MONTH,#eDate) THEN #eDate
ELSE EndDate
END AS EndDate
FROM cte
select
DateValue
, WeekStart =convert(date,(
case when datepart(week,DateValue) =1
then convert(date, (datename(year,DateValue) +'0101'))
else dateadd(day,##datefirst-datepart(weekday,DateValue)-(##datefirst-1),DateValue)
end) )
, WeekEnd =convert(date,(
case when datepart(week,DateValue) =53
then convert(date, (datename(year,DateValue) +'1231'))
else dateadd(day,(##datefirst)-datepart(weekday,DateValue)+(7-##datefirst),DateValue)
end) )
from dates
rextester demo: http://rextester.com/KYKS44588
test setup:
set datefirst 1;
declare #fromdate date = '20161227', #thrudate date = '20201231';
;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, dates as (
select top (datediff(day, #fromdate, #thrudate)+1)
[DateValue]=convert(date,dateadd(day,row_number() over(order by (select 1))-1,#fromdate))
from n as deka cross join n as hecto cross join n as kilo cross join n as [tenK]
order by 1
)
, test as (
select
convert(varchar(10),DateValue,120) as Date
, WeekStart = convert(varchar(10),convert(date,(
case when datepart(week,DateValue) =1
then convert(date, (datename(year,DateValue) +'0101'))
else dateadd(day,##datefirst-datepart(WeekDay,DateValue)-(##datefirst-1),DateValue)
end) ),120)
, WeekEnd = convert(varchar(10),convert(date,(
case when datepart(week,DateValue) =53
then convert(date, (datename(year,DateValue) +'1231'))
else dateadd(day,(##datefirst)-datepart(WeekDay,DateValue)+(7-##datefirst),DateValue)
end) ),120)
, week= datepart(week,DateValue)
from dates
)
select *
, dayname= datename(weekday,date)
, weekstartdayname= datename(weekday,weekstart)
, weekenddayname= datename(weekday,weekend)
from test
where week > 51
or week < 3
order by 1
results:
+------------+------------+------------+------+-----------+------------------+----------------+
| Date | WeekStart | WeekEnd | week | dayname | weekstartdayname | weekenddayname |
+------------+------------+------------+------+-----------+------------------+----------------+
| 2016-12-27 | 2016-12-26 | 2016-12-31 | 53 | Tuesday | Monday | Saturday |
| 2016-12-28 | 2016-12-26 | 2016-12-31 | 53 | Wednesday | Monday | Saturday |
| 2016-12-29 | 2016-12-26 | 2016-12-31 | 53 | Thursday | Monday | Saturday |
| 2016-12-30 | 2016-12-26 | 2016-12-31 | 53 | Friday | Monday | Saturday |
| 2016-12-31 | 2016-12-26 | 2016-12-31 | 53 | Saturday | Monday | Saturday |
| 2017-01-01 | 2017-01-01 | 2017-01-01 | 1 | Sunday | Sunday | Sunday |
| 2017-01-02 | 2017-01-02 | 2017-01-08 | 2 | Monday | Monday | Sunday |
| 2017-01-03 | 2017-01-02 | 2017-01-08 | 2 | Tuesday | Monday | Sunday |
| 2017-01-04 | 2017-01-02 | 2017-01-08 | 2 | Wednesday | Monday | Sunday |
| 2017-01-05 | 2017-01-02 | 2017-01-08 | 2 | Thursday | Monday | Sunday |
| 2017-01-06 | 2017-01-02 | 2017-01-08 | 2 | Friday | Monday | Sunday |
| 2017-01-07 | 2017-01-02 | 2017-01-08 | 2 | Saturday | Monday | Sunday |
| 2017-01-08 | 2017-01-02 | 2017-01-08 | 2 | Sunday | Monday | Sunday |
| 2017-12-18 | 2017-12-18 | 2017-12-24 | 52 | Monday | Monday | Sunday |
| 2017-12-19 | 2017-12-18 | 2017-12-24 | 52 | Tuesday | Monday | Sunday |
| 2017-12-20 | 2017-12-18 | 2017-12-24 | 52 | Wednesday | Monday | Sunday |
| 2017-12-21 | 2017-12-18 | 2017-12-24 | 52 | Thursday | Monday | Sunday |
| 2017-12-22 | 2017-12-18 | 2017-12-24 | 52 | Friday | Monday | Sunday |
| 2017-12-23 | 2017-12-18 | 2017-12-24 | 52 | Saturday | Monday | Sunday |
| 2017-12-24 | 2017-12-18 | 2017-12-24 | 52 | Sunday | Monday | Sunday |
| 2017-12-25 | 2017-12-25 | 2017-12-31 | 53 | Monday | Monday | Sunday |
| 2017-12-26 | 2017-12-25 | 2017-12-31 | 53 | Tuesday | Monday | Sunday |
| 2017-12-27 | 2017-12-25 | 2017-12-31 | 53 | Wednesday | Monday | Sunday |
| 2017-12-28 | 2017-12-25 | 2017-12-31 | 53 | Thursday | Monday | Sunday |
| 2017-12-29 | 2017-12-25 | 2017-12-31 | 53 | Friday | Monday | Sunday |
| 2017-12-30 | 2017-12-25 | 2017-12-31 | 53 | Saturday | Monday | Sunday |
| 2017-12-31 | 2017-12-25 | 2017-12-31 | 53 | Sunday | Monday | Sunday |
| 2018-01-01 | 2018-01-01 | 2018-01-07 | 1 | Monday | Monday | Sunday |
| 2018-01-02 | 2018-01-01 | 2018-01-07 | 1 | Tuesday | Monday | Sunday |
| 2018-01-03 | 2018-01-01 | 2018-01-07 | 1 | Wednesday | Monday | Sunday |
| 2018-01-04 | 2018-01-01 | 2018-01-07 | 1 | Thursday | Monday | Sunday |
| 2018-01-05 | 2018-01-01 | 2018-01-07 | 1 | Friday | Monday | Sunday |
| 2018-01-06 | 2018-01-01 | 2018-01-07 | 1 | Saturday | Monday | Sunday |
| 2018-01-07 | 2018-01-01 | 2018-01-07 | 1 | Sunday | Monday | Sunday |
| 2018-01-08 | 2018-01-08 | 2018-01-14 | 2 | Monday | Monday | Sunday |
| 2018-01-09 | 2018-01-08 | 2018-01-14 | 2 | Tuesday | Monday | Sunday |
| 2018-01-10 | 2018-01-08 | 2018-01-14 | 2 | Wednesday | Monday | Sunday |
| 2018-01-11 | 2018-01-08 | 2018-01-14 | 2 | Thursday | Monday | Sunday |
| 2018-01-12 | 2018-01-08 | 2018-01-14 | 2 | Friday | Monday | Sunday |
| 2018-01-13 | 2018-01-08 | 2018-01-14 | 2 | Saturday | Monday | Sunday |
| 2018-01-14 | 2018-01-08 | 2018-01-14 | 2 | Sunday | Monday | Sunday |
| 2018-12-24 | 2018-12-24 | 2018-12-30 | 52 | Monday | Monday | Sunday |
| 2018-12-25 | 2018-12-24 | 2018-12-30 | 52 | Tuesday | Monday | Sunday |
| 2018-12-26 | 2018-12-24 | 2018-12-30 | 52 | Wednesday | Monday | Sunday |
| 2018-12-27 | 2018-12-24 | 2018-12-30 | 52 | Thursday | Monday | Sunday |
| 2018-12-28 | 2018-12-24 | 2018-12-30 | 52 | Friday | Monday | Sunday |
| 2018-12-29 | 2018-12-24 | 2018-12-30 | 52 | Saturday | Monday | Sunday |
| 2018-12-30 | 2018-12-24 | 2018-12-30 | 52 | Sunday | Monday | Sunday |
| 2018-12-31 | 2018-12-31 | 2018-12-31 | 53 | Monday | Monday | Monday |
| 2019-01-01 | 2019-01-01 | 2019-01-06 | 1 | Tuesday | Tuesday | Sunday |
| 2019-01-02 | 2019-01-01 | 2019-01-06 | 1 | Wednesday | Tuesday | Sunday |
| 2019-01-03 | 2019-01-01 | 2019-01-06 | 1 | Thursday | Tuesday | Sunday |
| 2019-01-04 | 2019-01-01 | 2019-01-06 | 1 | Friday | Tuesday | Sunday |
| 2019-01-05 | 2019-01-01 | 2019-01-06 | 1 | Saturday | Tuesday | Sunday |
| 2019-01-06 | 2019-01-01 | 2019-01-06 | 1 | Sunday | Tuesday | Sunday |
| 2019-01-07 | 2019-01-07 | 2019-01-13 | 2 | Monday | Monday | Sunday |
| 2019-01-08 | 2019-01-07 | 2019-01-13 | 2 | Tuesday | Monday | Sunday |
| 2019-01-09 | 2019-01-07 | 2019-01-13 | 2 | Wednesday | Monday | Sunday |
| 2019-01-10 | 2019-01-07 | 2019-01-13 | 2 | Thursday | Monday | Sunday |
| 2019-01-11 | 2019-01-07 | 2019-01-13 | 2 | Friday | Monday | Sunday |
| 2019-01-12 | 2019-01-07 | 2019-01-13 | 2 | Saturday | Monday | Sunday |
| 2019-01-13 | 2019-01-07 | 2019-01-13 | 2 | Sunday | Monday | Sunday |
| 2019-12-23 | 2019-12-23 | 2019-12-29 | 52 | Monday | Monday | Sunday |
| 2019-12-24 | 2019-12-23 | 2019-12-29 | 52 | Tuesday | Monday | Sunday |
| 2019-12-25 | 2019-12-23 | 2019-12-29 | 52 | Wednesday | Monday | Sunday |
| 2019-12-26 | 2019-12-23 | 2019-12-29 | 52 | Thursday | Monday | Sunday |
| 2019-12-27 | 2019-12-23 | 2019-12-29 | 52 | Friday | Monday | Sunday |
| 2019-12-28 | 2019-12-23 | 2019-12-29 | 52 | Saturday | Monday | Sunday |
| 2019-12-29 | 2019-12-23 | 2019-12-29 | 52 | Sunday | Monday | Sunday |
| 2019-12-30 | 2019-12-30 | 2019-12-31 | 53 | Monday | Monday | Tuesday |
| 2019-12-31 | 2019-12-30 | 2019-12-31 | 53 | Tuesday | Monday | Tuesday |
| 2020-01-01 | 2020-01-01 | 2020-01-05 | 1 | Wednesday | Wednesday | Sunday |
| 2020-01-02 | 2020-01-01 | 2020-01-05 | 1 | Thursday | Wednesday | Sunday |
| 2020-01-03 | 2020-01-01 | 2020-01-05 | 1 | Friday | Wednesday | Sunday |
| 2020-01-04 | 2020-01-01 | 2020-01-05 | 1 | Saturday | Wednesday | Sunday |
| 2020-01-05 | 2020-01-01 | 2020-01-05 | 1 | Sunday | Wednesday | Sunday |
| 2020-01-06 | 2020-01-06 | 2020-01-12 | 2 | Monday | Monday | Sunday |
| 2020-01-07 | 2020-01-06 | 2020-01-12 | 2 | Tuesday | Monday | Sunday |
| 2020-01-08 | 2020-01-06 | 2020-01-12 | 2 | Wednesday | Monday | Sunday |
| 2020-01-09 | 2020-01-06 | 2020-01-12 | 2 | Thursday | Monday | Sunday |
| 2020-01-10 | 2020-01-06 | 2020-01-12 | 2 | Friday | Monday | Sunday |
| 2020-01-11 | 2020-01-06 | 2020-01-12 | 2 | Saturday | Monday | Sunday |
| 2020-01-12 | 2020-01-06 | 2020-01-12 | 2 | Sunday | Monday | Sunday |
| 2020-12-21 | 2020-12-21 | 2020-12-27 | 52 | Monday | Monday | Sunday |
| 2020-12-22 | 2020-12-21 | 2020-12-27 | 52 | Tuesday | Monday | Sunday |
| 2020-12-23 | 2020-12-21 | 2020-12-27 | 52 | Wednesday | Monday | Sunday |
| 2020-12-24 | 2020-12-21 | 2020-12-27 | 52 | Thursday | Monday | Sunday |
| 2020-12-25 | 2020-12-21 | 2020-12-27 | 52 | Friday | Monday | Sunday |
| 2020-12-26 | 2020-12-21 | 2020-12-27 | 52 | Saturday | Monday | Sunday |
| 2020-12-27 | 2020-12-21 | 2020-12-27 | 52 | Sunday | Monday | Sunday |
| 2020-12-28 | 2020-12-28 | 2020-12-31 | 53 | Monday | Monday | Thursday |
| 2020-12-29 | 2020-12-28 | 2020-12-31 | 53 | Tuesday | Monday | Thursday |
| 2020-12-30 | 2020-12-28 | 2020-12-31 | 53 | Wednesday | Monday | Thursday |
| 2020-12-31 | 2020-12-28 | 2020-12-31 | 53 | Thursday | Monday | Thursday |
+------------+------------+------------+------+-----------+------------------+----------------+
Calendar and Numbers table references:
Generate a set or sequence without loops - 1 - Aaron Bertrand
Generate a set or sequence without loops - 3 - Aaron Bertrand
The "Numbers" or "Tally" Table: What it is and how it replaces a loop - Jeff Moden
Creating a Date Table/Dimension in SQL Server 2008 - David Stein
Calendar Tables - Why You Need One - David Stein
Creating a date dimension or calendar table in SQL Server - Aaron Bertrand
TSQL Function to Determine Holidays in SQL Server - Tim Cullen
F_TABLE_DATE - Michael Valentine Jones
I'm trying to get a full result set of all combinations from 3 tables, but its not working because I can't figure out how to tell SQL-Server 2008 what I want.
I have simplified it down to the following similar problem... I have 3 tables:
table `date_ranges`:
--------------------------------------------------------
| start_day | end_day |
| -------------------------------------------------------- |
| November, 01 2015 14:37:00 | November, 02 2015 00:00:00 |
| November, 02 2015 00:00:00 | November, 03 2015 00:00:00 |
| November, 03 2015 00:00:00 | November, 04 2015 00:00:00 |
| November, 04 2015 00:00:00 | November, 04 2015 02:00:00 |
--------------------------------------------------------
table `sites`:
----
| site |
| ---- |
| 1 |
| 2 |
| 3 |
| 4 |
----
table `all_data`:
--------------------------------------
| data_date | data_site |
| ---------------------------|---------- |
| November, 02 2015 15:35:00 | 1 |
--------------------------------------
and I want to get the following result:
-------------------------------------------------------------------------------------------------------
| data_date | data_site | start_day | end_day | site |
| ------------------------------------------------------------------------------------------------------- |
| null | null | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 1 |
| November, 02 2015 15:35:00 | 1 | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 1 |
| null | null | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 1 |
| null | null | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 1 |
| null | null | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 2 |
| null | null | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 2 |
| null | null | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 2 |
| null | null | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 2 |
| null | null | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 3 |
| null | null | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 3 |
| null | null | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 3 |
| null | null | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 3 |
| null | null | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 4 |
| null | null | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 4 |
| null | null | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 4 |
| null | null | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 4 |
-------------------------------------------------------------------------------------------------------
But instead I can only figure out how to get the following result (see fiddle here):
-------------------------------------------------------------------------------------------------------
| data_date | data_site | start_day | end_day | site |
| ------------------------------------------------------------------------------------------------------- |
| null | null | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | null |
| November, 02 2015 15:35:00 | 1 | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 1 |
| null | null | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | null |
| null | null | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | null |
| null | null | null | null | 2 |
| null | null | null | null | 3 |
| null | null | null | null | 4 |
-------------------------------------------------------------------------------------------------------
using the following incorrect query:
select * from all_data d
full outer join date_ranges r on (r.start_day <= d.data_date and d.data_date < r.end_day)
full outer join sites s on s.site = d.data_site
A combination of CROSS JOIN and LEFT JOIN should do the trick for you.
Something like this..
SQL Fiddle
SELECT *
FROM DATE_RANGES R
CROSS JOIN SITES S
LEFT OUTER JOIN ALL_DATA D ON (R.START_DAY <= D.DATA_DATE AND D.DATA_DATE < R.END_DAY)
AND S.SITE = D.DATA_SITE