Sql server sql中的某些记录返回零

Sql server sql中的某些记录返回零,sql-server,Sql Server,我在存储过程中有一个查询,它计算表中的一些记录: SELECT t_Supporter.supporterID, COUNT(t_ticket.ticketID) AS totalTicket FROM t_Supporter INNER JOIN t_ticket ON t_Supporter.supporterID = t_ticket.supporterID_FK WHERE (t_Supporter.orgSectionID_FK = @orgID) GROUP BY t_S

我在存储过程中有一个查询,它计算表中的一些记录:

SELECT
  t_Supporter.supporterID,
  COUNT(t_ticket.ticketID) AS totalTicket
FROM t_Supporter
INNER JOIN t_ticket
  ON t_Supporter.supporterID = t_ticket.supporterID_FK
WHERE (t_Supporter.orgSectionID_FK = @orgID)
GROUP BY t_Supporter.supporterID

如果
t\u-Supporter.supporterID
t\u-ticket
中没有记录,我如何使我的查询返回0您需要使用条件聚合将
COUNT
更改为
SUM
,并将
内部联接
更改为
左外部联接

SELECT t_Supporter.supporterID, SUM(CASE WHEN t_ticket.ticketID IS NOT NULL THEN 1 ELSE 0 END) AS totalTicket
FROM t_Supporter LEFT OUTER JOIN
     t_ticket ON t_Supporter.supporterID = t_ticket.supporterID_FK
WHERE  (t_Supporter.orgSectionID_FK = @orgID)
GROUP BY t_Supporter.supporterID