oracle SQL 报告如何计算符合特定条件的记录数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5519978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:13:40  来源:igfitidea点击:

SQL reports how to count number of records that match a certain criteria

sqloraclepivotaggregate-functions

提问by Oscar Gomez

Say you have a table such as:

假设您有一张表,例如:

id   status  date
-----------------------
1    2       today
2    3       today
3    3       yesterday
4    2       yesterday
5    1       yesterday

And you want a query that counts the number or results that have the status 1, 2 or 3 for a given date, in the example the desired result set would be:

并且您需要一个查询来计算给定日期状态为 1、2 或 3 的数量或结果,在示例中所需的结果集是:

date        status 1   status 2  status 3 
            count      count     count 
------------------------------------------
today       0          1         1
yesterday   1          1         1

Can anyone point me in the right direction?. Thank you.

任何人都可以指出我正确的方向吗?谢谢你。

回答by Thomas

Select Date
    , Sum( Case When Status = 1 Then 1 Else 0 End ) Status1Count
    , Sum( Case When Status = 2 Then 1 Else 0 End ) Status2Count
    , Sum( Case When Status = 3 Then 1 Else 0 End ) Status3Count
From MyTable
Group By Date

回答by tster

select status, date, count(*)
from Table
group by status, date

result:

结果:

status date      count
  2    today     1
  3    today     1
  3    yesterday 1
  2    yesterday 1
  1    yesterday 1