简体   繁体   中英

SQL doing a count for a given date range

At the minute I'm doing query that counts the number of attempted transactions for a given date. But I want to make it so that I will be given output for a given date range, with each day listed individually.

select count(id)
from transactions
where service_id in ('1','2','3','4')
and date(created_at) = '2018-01-23';

As you can see, this will only return a count from one date. But how would I do this query for each date for a given range and print them one their own count column. If I can.

You should print them as separate rows not separate columns :

select date(created_at), count(id)
from transactions
where service_id in ('1', '2', '3', '4') and
     date(created_at) >= '2018-01-01' and
     date(created_at) < '2018-01-24'
group by date(created_at)
order by date(created_at);

You can use the group by and Having to achieve this.

select count(id), created_at
from transactions
where service_id in ('1','2','3','4')
grouby created_at
HAVING date(created_at) BETWEEN '2018-01-23' AND '2018-01-28';

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