2016-12-02 132 views
0

我使用以下SQL查询从MySQL数据库获取数据。每天总结数据的SQL查询

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

日期为例如2016.12.01. 10:51:58格式。

它应该在每天的基础上,因此例如2016.12.01. 10:51:582016.12.01. 15:51:58不应该是此查询中的不同条目。我应该如何按天分组?我想每天总结参与者的每项活动,而不是根据参与者的签名日期分开。

+0

你可以投的日期时间为日期。 – ZLK

+2

你使用的是MySQL还是SQL Server?而且,这些时间戳看起来不是以任何标准格式。他们是什么类型的? –

回答

1

你可以试试这个

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

请转换datetimedate

 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

我正在考虑start为您的日期字段,所以可能这可以帮助你。

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

而且您的日期格式不符合标准timestam匹配,所以请看看了点。可能在下面的参考也可以帮助你。

DATE_FORMAT(date,format)

MySQL: CONVERT

STR_TO_DATE()