On 3 Mar 2005 15:06:40 -0800,
ch****************@gmail.com wrote:
The SQL bellows shows what the output should look like. HOWEVER, the
code below makes use of a temp table containing all possible dates. My
question is, is there a better way to do this - one that doesn't
involve the temp table? Thanks in advance.
Instead of a temp table, you could create the needed dates on the fly:
CREATE VIEW Digits (NUM)
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 UNION ALL SELECT 8 UNION ALL
SELECT 9
GO
SELECT TempDate, LogText
FROM StationLog
RIGHT OUTER JOIN
(
SELECT dateadd(hour, 6 * NUM,
(select min(LogDate) FROM StationLog)) AS TempDate
FROM (
SELECT D1.NUM + 10* D10.NUM + 100* D100.NUM AS NUM
FROM DIGITS D1, DIGITS D10, DIGITS D100
) AS NUMBERS
WHERE NUM <= datediff(hour,
(select min(LogDate) FROM StationLog),
(select max(LogDate) FROM StationLog)) / 6
) AS Date_List
ON StationLog.LogDate = Date_List.TempDate
here's the output:
TempDate LogText
------------------------------------------------------ -----------
2005-01-01 00:00:00.000 entry one
2005-01-01 06:00:00.000 NULL
2005-01-01 12:00:00.000 entry two
2005-01-01 18:00:00.000 NULL
2005-01-02 00:00:00.000 entry three
2005-01-02 06:00:00.000 NULL
2005-01-02 12:00:00.000 NULL
2005-01-02 18:00:00.000 NULL
2005-01-03 00:00:00.000 entry four