我有以下查询来返回客户使用的作业(访问次数)以及他/她使用的独特审核。
SELECT
customer.users_id AS CustID,
COUNT(VisitID) AS Jobs,
COUNT(DISTINCT VetID) AS Resources
FROM customer
LEFT JOIN visit ON customer.users_id = visit.CustID
GROUP BY customer.users_id
当我添加WHERE子句以仅计算未重新计划或已取消的访问次数时,我的查询如下所示。
SELECT
customer.users_id AS CustID,
COUNT(VisitID) AS Jobs,
COUNT(DISTINCT VetID) AS Resources
FROM customer LEFT JOIN visit ON customer.users_id = visit.CustID
WHERE visit.Status != 'Cancelled' AND visit.Status != 'ReScheduled'
GROUP BY customer.users_id
但是,在这种情况下,我只会收到没有取消或重新安排访问的客户,虽然我正在为客户使用左连接,但我才知道。
我如何适应WHERE子句呢?
答案 0 :(得分:5)
您不需要WHERE
子句。这只会将LEFT JOIN
变为INNER JOIN
。您需要AND
上的LEFT JOIN
:
SELECT customer.users_id AS CustID, COUNT(VisitID) AS Jobs, COUNT(DISTINCT VetID) AS Resources
FROM customer
LEFT JOIN visit ON customer.users_id = visit.CustID
AND visit.Status NOT IN('Cancelled','ReScheduled')
GROUP BY customer.users_id
答案 1 :(得分:1)
当我们使用Format
添加where子句时,它的行为类似于DateTimePickerFormat.Custom
,其中过滤器应用于left outer join
子句之后。用inner join
子句
ON
后尝试
where