我希望记录能够毫无疑问地进入。 请帮我。谢谢你。
SELECT
ROUTE_ID,
ISNULL(COUNT(*), 0) AS TOTAL_CALL
FROM
dbo.tblCall_Report AR
CROSS JOIN
tblRoute R
INNER JOIN
tblNotification_Report BR ON R.ROUTE_ID = BR.RouteID
WHERE
CAST(BR.NotificationDateTime AS DATE) = '04.01.2017'
AND BR.CallNotification = 1
AND (BR.NotificationStatus = 1 OR BR.NotificationStatus IS NULL)
AND BR.RouteID IN (7434,7423)
GROUP BY
ROUTE_ID
tblCall_Report
NotificationDateTime
----------------------
2017-04-01
tblNotification_Report
NotificationDateTime RouteID
--------------------------------
2017-04-01 7434
结果:
ROUTE_ID | TOTAL_CALL
----------+-----------
7434 | 1
我想要这些结果:
ROUTE_ID | TOTAL_CALL
---------+-----------
7434 | 1
7423 | 0
答案 0 :(得分:0)
如果原始表中存在路由7423
,那么我的猜测是INNER JOIN
正在过滤此路由。改为使用LEFT JOIN
:
SELECT ROUTE_ID,
ISNULL(COUNT(*),0) AS TOTAL_CALL
FROM dbo.tblCall_Report AR
CROSS JOIN tblRoute R
LEFT JOIN tblNotification_Report BR
ON R.ROUTE_ID = BR.RouteID AND
BR.RouteID IN (7434, 7423)
WHERE CAST(BR.NotificationDateTime AS DATE) = '04.01.2017' AND
BR.CallNotification = 1 AND
(BR.NotificationStatus =1 OR BR.NotificationStatus IS NULL)
GROUP BY ROUTE_ID
答案 1 :(得分:0)
答案 2 :(得分:0)
你需要:
left join
on
R
在哪里,因为它肯定有价值
SELECT
ROUTE_ID,
CASE ISNULL(MIN(BR.RouteID), 0) WHEN 0 THEN 0 ELSE COUNT(*) END AS TOTAL_CALL
FROM
dbo.tblCall_Report AR
CROSS JOIN
tblRoute R
LEFT JOIN
tblNotification_Report BR ON R.ROUTE_ID = BR.RouteID
AND CAST(BR.NotificationDateTime AS DATE) = '04.01.2017'
AND BR.CallNotification = 1
AND (BR.NotificationStatus = 1 OR BR.NotificationStatus IS NULL)
GROUP BY
WHERE R.RouteID IN (7434,7423)
ROUTE_ID