我在这里找到了一些技巧,我曾经用它来更新我的查询,但我仍然无法到达那里: 这是我的疑问:
SELECT PUE.Name, puc.EventID, MONTH(DM.Date) AS Month, DAY(DM.Date) AS Day,
ISNULL(COUNT(puc.consumerdataID),0) AS Count
FROM Day_Map DM
LEFT JOIN PUConsumerData puc ON CONVERT(date,puc.date) = CONVERT(date,DM.Date)
LEFT JOIN PUEvents PUE on PUE.EventID = puc.EventID
WHERE CONVERT(date, DM.Date) >= '2013-10-18'
and CONVERT(date, DM.Date) <= '2013-10-20' and
GROUP BY pue.Name, puc.EventID, MONTH(DM.Date), DAY(DM.Date)
以下是它的回报:
Spring 3574 10 19 178
Spring 3574 10 20 33
Gusse 3575 10 18 5
Gusse 3575 10 19 117
Gusse 3575 10 20 18
Beach 3576 10 18 1
Beach 3576 10 19 133
Beach 3576 10 20 66
我想要它做的是为Spring添加零行:
Spring 3574 10 18 0
我添加了Day_Map表和日期,我看到了另一个提示,但我需要对名称进行分组,而不仅仅是日期,因此它不适用于我。其他想法?
以下是为示例输入的日期范围,但用户使用参数放入自定义日期范围。感谢。
答案 0 :(得分:1)
要获取日期和事件的产品,您需要使用cross join
select
dateevents.Name,
dateevents.EventID,
MONTH(dateevents.Date) AS Month,
DAY(dateevents.Date) AS Day,
COUNT(puc.consumerdataID) AS Count
from
(
select dm.date, pue.eventid, pue.name
from day_map dm
cross join puevents pue
where dm.date between '2013-10-18' and '2013-10-20'
) dateevents
LEFT JOIN puconsumerdata puc ON CONVERT(date,dateevents.Date) = CONVERT(date,puc.date)
and dateevents.eventid = puc.eventid
group by
dateevents.Name, dateevents.EventID, MONTH(dateevents.Date) , DAY(dateevents.Date)