0

I'm running the following query and getting an output mentioned below the SQL Query and is explained as follows:

Timestamp: Name of the column which is holding all date and time related values. MyDatabase: Name of the database Events: Name of another column with the name "Events" holding various values like, FIRST, SECOND,THIRD etc. I have mentioned FIRST here for convinience and clarity.

SELECT count(Timestamp) as COUNT,Timestamp 
FROM MyDatabase
WHERE EVENTS = "FIRST" GROUP BY Timestamp ;

For example, the output I'm getting is as follows:

     COUNT       TIMESTAMP
___________________________
1      1          2013-06-06 11:51:37.0

2      2          2013-06-06 11:51:39.0

3      2          2013-06-06 11:51:37.0

and so on....

After long list of occurance of date "2013-06-06", the date changes to "2013-06-07"

The output above is time specific but I want it to be date specific and write down my SQL query in such a way that my desired output should be as follows:

     COUNT       TIMESTAMP
___________________________
1      6          2013-06-06  ( Sum of all the "COUNT" values corresponding to the   date 2013-06-06)

2      8          2013-06-10  ( Sum of all the "COUNT" values corresponding to the date 2013-06-10 )

Could you please tell me what could be the modification required so that above output is achieved?

4

1 回答 1

1

select您需要在andgroup by子句中将时间戳转换为日期。在 MySQL 中,最简单的方法是使用以下date()函数:

SELECT count(Timestamp) as COUNT, date(Timestamp)
FROM MyDatabase
WHERE EVENTS = 'FIRST'
GROUP BY date(Timestamp)

如果您非常特别想要这种日期格式,您还可以使用date_format()

SELECT count(Timestamp) as COUNT, date_format(Timestamp, '%Y-%m-%d')
FROM MyDatabase
WHERE EVENTS = 'FIRST'
GROUP BY date_format(Timestamp, '%Y-%m-%d')
于 2013-07-16T00:45:51.977 回答