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

前端 未结 7 1785
庸人自扰
庸人自扰 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 13:48

    When you use a group by, it only creates a distinct list of values that exist in your records. Since 20140228 has no records, it will not show up in the group by.

    Your best bet is to generate a list of values, dates in your case, and left join or apply that table against your history table.

    I can't seem to copy my T-SQL in here so here's a hastebin.

    http://hastebin.com/winaqutego.vbs

提交回复
热议问题