简体   繁体   中英

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

We have a table which inserts a new row each time something happens.

We basically want to do a 7, 14, 28 day thing based on the query:

We want to work with these fields:

status, time (datetime)

We basically want to group by the day, and echo a count for each day:

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

Kind of like this, so it groups by day, and shows it by status and count .

There are only 3 statuses, the ones listed: completed, aborted, failed

We want to get the counts for each of these statuses, and group them by status and by day - for a certain day range, example: 7, 14, 21, 28

How can we do this using SQL? I remember doing something like this a long time ago.

something like this should help you:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM