Replacing NULL with 0 in a SQL server query

后端 未结 12 1193
情书的邮戳
情书的邮戳 2020-11-28 02:39

I have developed a query, and in the results for the first three columns I get NULL. How can I replace it with 0?

  Select c.rund         


        
12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 03:11

    When you say the first three columns, do you mean your SUM columns? If so, add ELSE 0 to your CASE statements. The SUM of a NULL value is NULL.

    sum(case when c.runstatus = 'Succeeded' then 1 else 0 end) as Succeeded, 
    sum(case when c.runstatus = 'Failed' then 1 else 0 end) as Failed, 
    sum(case when c.runstatus = 'Cancelled' then 1 else 0 end) as Cancelled, 
    
    • SQL Fiddle Demo

提交回复
热议问题