How to return a default value when no rows are returned from the select statement

前端 未结 7 1850
庸人自扰
庸人自扰 2021-01-22 13:26

I have a select statement that returns two columns, a date column, and a count(value) column. When the count(value) column doesn\'t have any records,

7条回答
  •  梦谈多话
    2021-01-22 14:09

    I agree that a Dates table (AKA time dimension) is the right solution, but there is a simpler option:

    SELECT
        CONVERT(VARCHAR(25), DateTime, 101) AS RecordDate,
        SUM(CASE WHEN Value < 700 THEN 1 ELSE 0 END) AS RecordCount
    FROM
        History
    GROUP BY
        CONVERT(VARCHAR(25), DateTime, 101)
    

提交回复
热议问题