簡體   English   中英

MySQL查詢-在一段時間內按狀態和日期分組?

[英]mySQL query - group by status & day, for a certain period?

我們有一個表格,每當發生某種情況時,都會插入一個新行。

我們基本上想根據查詢做7、14、28天的事情:

我們想與這些領域合作:

status, time (datetime)

我們基本上想按天分組,並回顯每天的計數:

5th October
Completed   5,292
Aborted 19
Failed 1

4th October
Completed   5,292
Aborted 19
Failed 1

3rd October
Completed   5,292
Aborted 19
Failed 1

有點像這樣,所以它按天分組,並按statuscount

只有3種狀態,列出的狀態:已完成,已中止,失敗

我們想要獲取每個狀態的計數,並按狀態和日期將它們分組-在特定的日期范圍內,例如:7、14、21、28

我們如何使用SQL做到這一點? 我記得很久以前就做過這樣的事情。

這樣的事情應該可以幫助您:

Declare @StartDate as smalldatetime
Declare @range as int

set @StartDate = getDate() --or whatever else you want to use
set @range = 7

Select status, Count(time)
Group By status
where time >= dateadd(dd, -@range, @StartDate)

暫無
暫無

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

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