1

I've found a few tips here which I used to update my query but I'm still unable to get there: Here's my query:

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)

Here's what it returns:

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

What I want it to do is add the zero line for Spring like this:

Spring  3574    10  18  0

I added the Day_Map table with the dates, per another tip I saw but I need to group on the name and not just the date so its not working for me. Other ideas?

Here's the date range is entered for an example but the user uses parameters to put in a custom date range. Thanks.

4

1 回答 1

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)
于 2013-10-20T18:48:09.623 回答