Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating filler dates

Author  Topic 

adriane
Starting Member

7 Posts

Posted - 2008-01-15 : 16:23:22
Not sure that's what I need to do but here goes: I need to create a temp table with data for a crosstab. (columns grouped by month)

The crosstab's not gonna show a missing month (example, date range is from January to June but there's no data for the month of January, so the 1rst column in the crosstab is Feb)
Is there a way to create 'dummy' dates in the select statement and insert them in my result set so missing months will show whether theres data or not?

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-15 : 16:29:16
Join to the following derived table:
SELECT * FROM (
SELECT CAST(n0 + n1 + n2 + n3 + n4 + n5 + n6 + n7 AS DATETIME) AS date
FROM (SELECT 0 AS n0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3) AS z0
CROSS JOIN (SELECT 0 AS n1 UNION SELECT 4 UNION SELECT 8 UNION SELECT 12) AS z1
CROSS JOIN (SELECT 0 AS n2 UNION SELECT 16 UNION SELECT 32 UNION SELECT 48) AS z2
CROSS JOIN (SELECT 0 AS n3 UNION SELECT 64 UNION SELECT 128 UNION SELECT 192) AS z3
CROSS JOIN (SELECT 0 AS n4 UNION SELECT 256 UNION SELECT 512 UNION SELECT 768) AS z4
CROSS JOIN (SELECT 0 AS n5 UNION SELECT 1024 UNION SELECT 2048 UNION SELECT 3072) AS z5
CROSS JOIN (SELECT 0 AS n6 UNION SELECT 4096 UNION SELECT 8192 UNION SELECT 12288) AS z6
CROSS JOIN (SELECT 0 AS n7 UNION SELECT 16384 UNION SELECT 32768 UNION SELECT 49152) AS z7 ) dates
WHERE dates.date >= '20070201'
AND dates.date < DATEADD(d,DATEDIFF(d,0,GETDATE()),0)
ORDER BY dates.date


"...database development, while a serious pursuit and vitally important to business, should be fun!"
-Adam Machanic
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-01-15 : 16:49:12
You can use the function on the link below.

Date Table Function F_TABLE_DATE:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519


CODO ERGO SUM
Go to Top of Page

adriane
Starting Member

7 Posts

Posted - 2008-01-16 : 11:50:13
Thank you very much. I'de never even heard of a cross join. The solution worked great!
Go to Top of Page
   

- Advertisement -