簡體   English   中英

SQL報告如何計算符合特定條件的記錄數

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

假設您有一個表格,例如:

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

並且您想要一個查詢,該查詢計算給定日期的狀態為1、2或3的數字或結果,在該示例中,所需結果集為:

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

誰能指出我正確的方向? 謝謝。

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
select status, date, count(*)
from Table
group by status, date

結果:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM