Count函数返回空记录集需要它返回0

时间:2016-02-25 16:54:31

标签: sql-server

我有这个问题......

SELECT COUNT(*) AS dupes
FROM documents d JOIN mv_compgift g ON g.documentId = d.id
   JOIN mv_donorid di ON di.documentId = d.id
   JOIN mv_appealcode ac ON ac.documentId = d.id
GROUP BY di.value,ac.value,d.dateDeleted
HAVING COUNT(*) > 1
AND g.value = 'Y' 
AND d.dateDeleted IS NULL 
AND di.value = '0621839010'
AND ac.value = 'AD62Q'

当我有一个空的记录集时,我需要它显示0。

我看过并尝试了不同的功能,如合并,但没有任何作品......请帮助。感谢

2 个答案:

答案 0 :(得分:2)

  <xsl:template match="hexagon:PointCode
                         [following::hexagon:PointCode[1]
                          /@codeLinework = 'none'
                          or
                          following::hexagon:Point[1]
                          @lineworkFlag='START LINJE']">
     do something...
  </xsl:template>

答案 1 :(得分:1)

SELECT
    COUNT(*), -- raw value
    CASE COUNT(*)
        WHEN 1 THEN 'OK' WHEN 0 THEN 'NOTHING' ELSE 'DUPS'
    END as HasDups -- translated value
FROM documents d JOIN mv_compgift g ON g.documentId = d.id
   JOIN mv_donorid di ON di.documentId = d.id
   JOIN mv_appealcode ac ON ac.documentId = d.id
WHERE g.value = 'Y' AND d.dateDeleted IS NULL 
    AND di.value = '0621839010' AND ac.value = 'AD62Q'

您的HAVING条件正在消除没有重复的群组。没有办法让零回归。

另外,我很惊讶g.value过滤器没有错误。无论如何,所有这些条件似乎都属于WHERE而非HAVING