我有以下查询
select dc.dateasdate as 'Date', count(Practice) as 'Total Enquiries', dp.practice, de.Origin
from dim.Practice dp
left join fact.enquiry fe on fe.PracticeSKey = dp.PracticeSKey
left join dim.EnquiryOrigin de on de.EnquiryOriginSKey = fe.EnquiryOriginSKey and de.Origin = 'Web Enquiry'
left join dim.Calendar dc on dc.CalendarSKey = fe.EnquiryCreatedSKey
where dc.dateasdate = '2016-04-03'
group by dc.dateasdate , de.Origin,practice
order by dc.dateasdate ,Practice
当有Practice
时,它不会显示。
任何人都可以帮助我吗?我已经坚持了几天。
答案 0 :(得分:5)
WHERE
条件有效地将LEFT JOIN
变为INNER JOIN
。您应该将条件放在最后LEFT JOIN
:
SELECT
dc.dateasdate AS 'Date',
COUNT(Practice) AS 'Total Enquiries',
dp.practice,
de.Origin
FROM dim.Practice dp
LEFT JOIN fact.enquiry fe
ON fe.PracticeSKey = dp.PracticeSKey
LEFT JOIN dim.EnquiryOrigin de
ON de.EnquiryOriginSKey = fe.EnquiryOriginSKey
AND de.Origin = 'Web Enquiry'
LEFT JOIN dim.Calendar dc
ON dc.CalendarSKey = fe.EnquiryCreatedSKey
AND dc.dateasdate = '2016-04-03'
GROUP BY
dc.dateasdate,
de.Origin,
practice
ORDER BY dc.dateasdate, Practice