我被困在一个查询中。其实我想减少我正在使用的选择数量。我想计算每个场景的行数。
表格列的描述是 -
此处month
是一年中的一个月
Name
是人物名称
需要注意的是他们是否需要注意。我们有条件 -
如果需要注意“N' N'并且isdate(Date)= 0然后它来No Attention required.
如果需要注意的是“N' N'并且isdate(Date)= 1然后它来Attention Completed
如果需要注意的是' Y' (然后不需要考虑日期列)它来自Attention Required
日期只是需要医疗护理的日期,可以是空或任何日期
局外人 - IF' 0'然后是来自其他国家的外国人。对于局外人,同样的规则适用于需要注意。只是国旗将区分内幕和外线。
以下是示例表
Month Name Attention Required Y/N Date Outsider
January A N 2015-01-02 0
January B N Null 0
January C Y Null 0
January D Y 2015-01-20 1
February E Y 2015-02-01 1
February F N null 0
February G Y null 0
February H N 2015-02-21 1
February I N null 0
March J Y null 1
March K N 2015-03-08 1
March L N null 0
March M Y null 1
March N N null 1
April O N 2014-04-04 1
April P Y null 0
April Q N 2015-04-10 0
April R Y null 0
April S Y null 1
我想要这种格式的输出 -
Month Insider Insider Total Outsider Outsider Total Grand Total
No Attention Required Attention Completed Attention Required No Attention Required Attention Completed Attention Required
January 1 1 1 3 0 0 1 2 4
February 2 0 1 3 0 0 1 2 5
March 1 0 0 1 1 1 2 4 5
April 0 1 2 3 0 1 1 2 5
Grand Total 4 2 4 10 1 3 5 9 19
所以我无法减少选择的号码。对于每列,我不能使用不同的选择查询。我正在使用这些查询来按月查找计数。
For insiders-
select Month, count(Name) as No_Attention_Required
FROM sample where Attention_Required ='N' and isdate(Date)=0
and Outsider='0' group by Month
select Month, count(Name) as Attention_Completed
FROM sample where Attention_Required ='N' and isdate(Date)=1
and Outsider='0' group by Month
select Month, count(Name) as Attention_Required
FROM sample where Attention_Required ='Y'
and Outsider='0' group by Month
select Month, count(Name) as Insider_Total
FROM sample where Outsider='0' group by Month
For Outsiders-
select Month, count(Name) as No_Attention_Required
FROM sample where Attention_Required ='N' and isdate(Date)=0
and Outsider='1' group by Month
select Month, count(Name) as Attention_Completed
FROM sample where Attention_Required ='N' and isdate(Date)=1
and Outsider='1' group by Month
select Month, count(Name) as Attention_Required
FROM sample where Attention_Required ='Y'
and Outsider='1' group by Month
select Month, count(Name) as Outsider_Total
FROM sample where Outsider='1' group by Month
之后我计划在一个月内加入他们。
我需要帮助减少选择这个数量。任何帮助将不胜感激。提前致谢!
EDITED:
这里我是我的示例案例陈述
select Month,case when Attention_Required='N' and isdate(Date)=0 then count(Name) end as Attention_Not_Needed,
case when Attention_Required='N' and isdate(Date)=1 then count(Name) end as Attention_Completed
FROM sample where Attention_Required='N'
and Outsider='0' group by Month,Attention_Required,Date
答案 0 :(得分:3)
您可以使用CASE
一次性获取数字,也可以使用ROLLUP
获取摘要;
SELECT
Month,
COUNT(CASE WHEN Attention_Required = 'N' AND ISDATE(date) = 0 AND outsider = 0 THEN 1 END) AS I_NAR,
COUNT(CASE WHEN Attention_Required = 'N' AND ISDATE(date) = 1 AND outsider = 0 THEN 1 END) AS I_AC,
COUNT(CASE WHEN Attention_Required = 'Y' AND outsider = 0 THEN 1 END) AS I_AR,
COUNT(CASE WHEN outsider = 0 THEN 1 END) AS I_TOTAL,
COUNT(CASE WHEN Attention_Required = 'N' AND ISDATE(date) = 0 AND outsider = 1 THEN 1 END) AS O_NAR,
COUNT(CASE WHEN Attention_Required = 'N' AND ISDATE(date) = 1 AND outsider = 1 THEN 1 END) AS O_AC,
COUNT(CASE WHEN Attention_Required = 'Y' AND outsider = 1 THEN 1 END) AS O_AR,
COUNT(CASE WHEN outsider = 0 THEN 1 END) AS O_TOTAL,
COUNT(1) AS GRAND_TOTAL
FROM sample
GROUP BY ROLLUP(Month)
...给出了结果;
Month I_NAR I_AC I_AR I_TOTAL O_NAR O_AC O_AR O_TOTAL GRAND_TOTAL
-------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
January 1 1 1 3 0 0 1 3 4
February 2 0 1 3 0 1 1 3 5
March 1 0 0 1 1 1 2 1 5
April 0 1 2 3 0 1 1 3 5
NULL 4 2 4 10 1 3 5 10 19
答案 1 :(得分:1)
您没有正确使用CASE语句。以下是您应该如何使用它:
select Month,
SUM(case when Attention_Required='N' and isdate(Date)=0 then 1 ELSE 0 end) as Attention_Not_Needed
, SUM(case when Attention_Required='N' and isdate(Date)=1 then 1 ELSE 0 end) as Attention_Completed
FROM sample
where Outsider='0'
group by Month