سؤال

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?

هل كانت مفيدة؟

المحلول

You need to convert the timestamp to a date, in both the select and group by clauses. In MySQL, the easiest way to do this is with the date() function:

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

If you very specifically want that format of date, you can also use 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')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top