在case语句中使用sql count

时间:2013-07-31 15:58:31

标签: sql

我有一张桌子,我需要以下列方式呈现输出。

tb_a:

col1  |  reg_id | rsp_ind 

rsp_ind = 0的行数为“New”,1为“Accepted”

输出应为

NEW | Accepted
9   | 10

我尝试使用以下查询。

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

我的输出为

NEW | Accepted
NULL| 10
9   | NULL

有人可以帮我调整查询以实现输出。 注意:我无法在此处添加总和。它是一个更大的程序的一部分,所以我不能添加一个超级查询。

8 个答案:

答案 0 :(得分:87)

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

您可以看到此请求的输出HERE

答案 1 :(得分:9)

关闭...尝试:

select 
   Sum(case when rsp_ind = 0 then 1 Else 0 End) as 'New',
   Sum(case when rsp_ind = 1 then 1 else 0 end) as 'Accepted'
from tb_a

答案 2 :(得分:5)

根据您的SQL风格,您也可以imply the else statement汇总计数。

例如,这是一个简单的表Grades

| Letters |
|---------|
| A       |
| A       |
| B       |
| C       |

我们可以像这样(Interactive Demo in SQL Fiddle)测试每种汇总计数器语法:

SELECT
    COUNT(CASE WHEN Letter = 'A' THEN 1 END)           AS [Count - End],
    COUNT(CASE WHEN Letter = 'A' THEN 1 ELSE NULL END) AS [Count - Else Null],
    COUNT(CASE WHEN Letter = 'A' THEN 1 ELSE 0 END)    AS [Count - Else Zero],
    SUM(CASE WHEN Letter = 'A' THEN 1 END)             AS [Sum - End],
    SUM(CASE WHEN Letter = 'A' THEN 1 ELSE NULL END)   AS [Sum - Else Null],
    SUM(CASE WHEN Letter = 'A' THEN 1 ELSE 0 END)      AS [Sum - Else Zero]
FROM Grades

以下是结果(为便于阅读,unpivoted):

|    Description    | Counts |
|-------------------|--------|
| Count - End       |    2   |
| Count - Else Null |    2   |
| Count - Else Zero |    4   | *Note: Will include count of zero values
| Sum - End         |    2   |
| Sum - Else Null   |    2   |
| Sum - Else Zero   |    2   |

Aggregate Functions in SQL的文档保持一致

COUNT的文档:

  

COUNT(*)-返回组中的项目数。这包括NULL值和重复项。
  COUNT(ALL expression)-计算组中每一行的表达式,并返回非空值的数量。
  COUNT(DISTINCT expression)-计算组中每一行的表达式,并返回唯一的非空值的数量。

SUM的文档:

  

ALL-将聚合函数应用于所有值。 ALL是默认设置。
  DISTINCT-指定SUM返回唯一值的总和。

答案 3 :(得分:2)

您获得两行而不是一行的原因是您在外部查询中按rsp_ind进行分组(您没有,我很失望,与我们分享)。在没有处理GROUP BY项目的情况下,没有什么可以强制执行一行而不是两行。

答案 4 :(得分:1)

如果要基于列对结果进行分组并根据相同的结果进行计数,则可以按以下方式运行查询:

$ sql =“ SELECT列名,

COUNT(CASE WHEN COLUMNNAME IN ('YOURCONDITION') then 1 ELSE NULL END) as 'New',

COUNT(CASE WHEN COLUMNNAME IN ('YOURCONDITION') then 1 ELSE NULL END) as 'ACCPTED',

来自TABLENAME

GROUP BY COLUMNANME”;

答案 5 :(得分:0)

select sum(rsp_ind = 0) as `New`,
       sum(rsp_ind = 1) as `Accepted` 
from tb_a

答案 6 :(得分:0)

好。我解决了

SELECT `smart_projects`.project_id, `smart_projects`.business_id, `smart_projects`.title,
 `page_pages`.`funnel_id` as `funnel_id`, count(distinct(page_pages.page_id) )as page_count, count(distinct (CASE WHEN page_pages.funnel_id != 0 then  page_pages.funnel_id ELSE NULL END ) ) as funnel_count
FROM `smart_projects`
LEFT JOIN `page_pages` ON `smart_projects`.`project_id` = `page_pages`.`project_id`
WHERE  smart_projects.status !=  0 
AND `smart_projects`.`business_id` = 'cd9412774edb11e9'
GROUP BY `smart_projects`.`project_id`
ORDER BY `title` DESC

答案 7 :(得分:0)

CREATE TABLE #CountMe (Col1 char(1));

INSERT INTO #CountMe VALUES ('A');
INSERT INTO #CountMe VALUES ('B');
INSERT INTO #CountMe VALUES ('A');
INSERT INTO #CountMe VALUES ('B');

SELECT
    COUNT(CASE WHEN Col1 = 'A' THEN 1 END) AS CountWithoutElse,
    COUNT(CASE WHEN Col1 = 'A' THEN 1 ELSE NULL END) AS CountWithElseNull,
    COUNT(CASE WHEN Col1 = 'A' THEN 1 ELSE 0 END) AS CountWithElseZero
FROM #CountMe;