Count Returning blank instead of 0

前端 未结 3 498
面向向阳花
面向向阳花 2020-12-06 09:52

Good day everyone. Here is my code:

SELECT 
    \'Expired Item -\'+ DateName(mm,DATEADD(MM,4,AE.fld_LOAN)) as [Month]
    ,COUNT(PIT.fld_ID)\'COUNT\'
    ,SUM         


        
3条回答
  •  伪装坚强ぢ
    2020-12-06 10:53

    You cannot expect any records to be outputted when using a GROUP BY clause, when no records exist in your source.

    If you want an output of 0 from the SUM and COUNT functions, then you should not use GROUP BY.

    The reason is that when you have no records, the GROUP BY clause have nothing to group by, and then is not able to give you any output.

    For example:

    SELECT COUNT(*) FROM (SELECT 'Dummy' AS [Dummy] WHERE 1 = 0) DummyTable
    

    will return one record with the value '0', where as:

    SELECT COUNT(*) FROM (SELECT 'Dummy' AS [Dummy] WHERE 1 = 0) DummyTable
    GROUP BY [Dummy]
    

    will return no records.

提交回复
热议问题