嘿所有我使用总和iff根据多个标准返回计数。
我基本上运行一个关于每个站点收到的呼叫的报告,但是我需要在结果集中包含0个呼叫的站点,如果他们没有那个星期的呼叫,则值为0或甚至为Null。
唯一的问题是我的cluase只包括本周有电话的网站
任何想法。
代码:
SELECT
d.sitename,
count(c.Chargeablecalls) AS All_Calls,
SUM(IIf(c.ChargeableCalls Like "Chargeable",1,0)) AS Chargeable_calls,
d.sitetype
FROM
(Callstatus AS s LEFT JOIN statusconversion AS c ON s.description=c.reportheading)
INNER JOIN sitedetails AS d ON s.zone=d.zone
WHERE s.date_loaded BETWEEN
(SELECT reportdate FROM reportMonth) AND (SELECT priorweek FROM reportMonth)
GROUP BY d.sitename, d.sitetype;
答案 0 :(得分:0)
RIGHT JOIN
需要sitedetails
才能获得所有网站,即使是那些没有通话的网站。
您可能需要单独执行查询的前半部分,然后在主查询中使用该查询。
答案 1 :(得分:0)
create a new query - qryCallStatus:
SELECT DISTINCT zone, description
FROM Callstatus, reportMonth
WHERE
Callstatus.date_loaded BETWEEN reportMonth.reportdate AND reportMonth.priorweek;
Then change your output query to:
SELECT
d.sitename,
count(c.Chargeablecalls) AS All_Calls,
SUM(IIf(c.ChargeableCalls Like "Chargeable",1,0)) AS Chargeable_calls,
d.sitetype
FROM
(sitedetails AS d LEFT JOIN qryCallStatus AS s ON d.zone=s.zone)
LEFT JOIN statusconversion AS c ON s.description=c.reportheading
GROUP BY d.sitename, d.sitetype;