SQL query to summarize data on daily basis

时间:2016-12-02 05:14:39

标签: php mysql sql

I use following SQL query to get data from MySQL database.

select
    classid,start,
    count(case when classid='Handball' then 1 end) as handball_count
from signature where classid='Handball'
group by start

Date is for example 2016.12.01. 10:51:58 format.

It should be on a daily basis, so for example 2016.12.01. 10:51:58 and 2016.12.01. 15:51:58 shouldn't be a different entry in this query. How should I group by day? I would like to summarize participants for each activity per day and not separated by the date of the participant's signature.

3 个答案:

答案 0 :(得分:1)

please convert datetime to date

     SELECT CONVERT(date, date)
or
GROUP BY CAST(date AS DATE)


select
    classid,CONVERT(date, date),
    count(case when classid='Handball' then 1 end) as handball_count
from signature where classid='Handball'
group by date,classid

答案 1 :(得分:1)

you can try this

SELECT count(case when classid='Handball' then 1 end) as handball_count,
   classid,DATE(start) DateOnly 
FROM signature where classid='Handball'
GROUP BY DateOnly;

答案 2 :(得分:1)

I am considering start as your date field, so might be this can help you.

select
    classid,CONVERT(start, date),
    count(case when classid='Handball' then 1 end) as handball_count
from signature where classid='Handball'
group by start

Also your date format doesn't match with standard timestam so please take a look for that. Might be below reference can also help you.

DATE_FORMAT(date,format)

MySQL: CONVERT

STR_TO_DATE()