OGeek|极客世界-中国程序员成长平台

标题: using sql count in a case statement [打印本页]

作者: 菜鸟教程小白    时间: 2022-5-24 13:56
标题: using sql count in a case statement

I have a table and i need to present the output in the following fashion.

tb_a:

col1  |  reg_id | rsp_ind 

Count of rows with rsp_ind = 0 as 'New' and 1 as 'Accepted'

The output should be

NEW | Accepted
9   | 10

I tried using the following query.

select 
  case when rsp_ind = 0 then count(reg_id)end as 'New',
  case when rsp_ind = 1 then count(reg_id)end as 'Accepted'
from tb_a

and i m getting output as

NEW | Accepted
NULL| 10
9   | NULL

Could someone help to me tweak the query to achieve the output. Note : I cannot add a sum surrounding this. Its part of a bigger program and so i cannot add a super-query to this.



Best Answer-推荐答案


SELECT 
    COUNT(CASE WHEN rsp_ind = 0 then 1 ELSE NULL END) as "New",
    COUNT(CASE WHEN rsp_ind = 1 then 1 ELSE NULL END) as "Accepted"
from tb_a

You can see the output for this request HERE






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4