我附上了这些结果,我想要的只是那些日子和时间。小时和没有计数的天数记录。
感谢下面是我使用的查询
SELECT dateadd(hour, datediff(hour, 0, date_sent_to_adobe), 0) as TimeStampHour, Count(*) 'Total Count this Hour'
FROM [Publication_Data_Management].[AdobeExtract].[Membership_Detail]
GROUP BY dateadd(hour, datediff(hour, 0, date_sent_to_adobe), 0)
ORDER BY dateadd(hour, datediff(hour, 0, date_sent_to_adobe), 0) desc
答案 0 :(得分:0)
您可以生成所有行,然后使用left join
和group by
来引入行。这是一种方法:
SELECT DATEADD(hour, h.hr, d.dte) as TimeStampHour,
Count(*) as total_count
FROM (SELECT DISTINCT CAST(date_sent_to_adobe as DATE) as dte
FROM Publication_Data_Management.AdobeExtract.Membership_Detail
) d CROSS JOIN
(VALUE (0), (1), (2), . . ., (23) ) as h(hr) LEFT JOIN
Publication_Data_Management.AdobeExtract.Membership_Detail md
ON md.date_sent_to_adobe >= DATEADD(hour, h.hr, d.dte) AND
md.date_sent_to_adobe < DATEADD(hour, h.hr + 1, d.dte)
GROUP BY DATEADD(hour, h.hr, d.dte)
ORDER BY DATEADD(hour, h.hr, d.dte) DESC;