patternModerate
Insert Missing Dates from a query
Viewed 0 times
insertdatesquerymissingfrom
Problem
How can i insert missing dates from a query i created. The result below:
I want the result to have missing dates with 0 value as shown below:
Please note that I have read only access to the server.
Date Frequency
2014-05-18 5
2014-05-20 7
2014-05-25 7
2014-05-27 6I want the result to have missing dates with 0 value as shown below:
Date Frequency
2014-05-18 5
2014-05-19 0
2014-05-20 7
2014-05-21 0
2014-05-22 0
2014-05-23 0
2014-05-24 0
2014-05-25 7
2014-05-26 0
2014-05-27 6Please note that I have read only access to the server.
Solution
Here is an example using a calendar table (which you really should have). This example just populates 2014 but you can stuff it with as many years as you like...
Now the query is simple:
db<>fiddle example
If you can't create a calendar table (and don't have a numbers table handy either), then you can just put it inline:
db<>fiddle example
For more on generating sets (of dates, numbers, etc.) see this series:
And more on calendar tables:
CREATE TABLE dbo.Calendar(d DATE PRIMARY KEY);
INSERT dbo.Calendar(d) SELECT TOP (365)
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY number)-1, '20140101')
FROM [master].dbo.spt_values
WHERE [type] = N'P' ORDER BY number;Now the query is simple:
DECLARE @s DATE = '20140518', @e DATE = '20140527';
SELECT c.d, Frequency = COALESCE(s.Frequency,0)
FROM dbo.Calendar AS c
LEFT OUTER JOIN dbo.splunge AS s
ON c.d = s.[date]
WHERE c.d >= @s
AND c.d < DATEADD(DAY, 1, @e);db<>fiddle example
If you can't create a calendar table (and don't have a numbers table handy either), then you can just put it inline:
DECLARE @s DATE = '20140518', @e DATE = '20140527';
SELECT c.d, Frequency = COALESCE(s.Frequency,0)
FROM
(
SELECT TOP (DATEDIFF(DAY, @s, @e)+1)
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY number)-1, @s)
FROM [master].dbo.spt_values
WHERE [type] = N'P' ORDER BY number
) AS c(d)
LEFT OUTER JOIN dbo.splunge2 AS s
ON c.d = s.[date]
WHERE c.d >= @s
AND c.d < DATEADD(DAY, 1, @e);db<>fiddle example
For more on generating sets (of dates, numbers, etc.) see this series:
- Generate a set or sequence without loops – part 1
- Generate a set or sequence without loops – part 2
- Generate a set or sequence without loops – part 3
And more on calendar tables:
- Creating a date dimension or calendar table
- Using a Calendar Table - Part 1
Code Snippets
CREATE TABLE dbo.Calendar(d DATE PRIMARY KEY);
INSERT dbo.Calendar(d) SELECT TOP (365)
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY number)-1, '20140101')
FROM [master].dbo.spt_values
WHERE [type] = N'P' ORDER BY number;DECLARE @s DATE = '20140518', @e DATE = '20140527';
SELECT c.d, Frequency = COALESCE(s.Frequency,0)
FROM dbo.Calendar AS c
LEFT OUTER JOIN dbo.splunge AS s
ON c.d = s.[date]
WHERE c.d >= @s
AND c.d < DATEADD(DAY, 1, @e);DECLARE @s DATE = '20140518', @e DATE = '20140527';
SELECT c.d, Frequency = COALESCE(s.Frequency,0)
FROM
(
SELECT TOP (DATEDIFF(DAY, @s, @e)+1)
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY number)-1, @s)
FROM [master].dbo.spt_values
WHERE [type] = N'P' ORDER BY number
) AS c(d)
LEFT OUTER JOIN dbo.splunge2 AS s
ON c.d = s.[date]
WHERE c.d >= @s
AND c.d < DATEADD(DAY, 1, @e);Context
StackExchange Database Administrators Q#75473, answer score: 15
Revisions (0)
No revisions yet.