tags:

views:

55

answers:

5

In the table below

+-------+-----------------------+ 
| id    | timestamp             | 
+-------+-----------------------+ 
| 1     | 2010-06-10 14:35:30   | 
| 2     | 2010-06-10 15:27:35   | 
| 3     | 2010-06-10 16:39:36   | 
| 4     | 2010-06-11 14:55:30   | 
| 5     | 2010-06-11 18:45:31   | 
| 6     | 2010-06-12 20:25:31   | 
+-------+-----------------------+ 

I want to be able to count the dates (time is ignored). So the output should be like below:

+-------+-----------------------+ 
| id    | type         | count  |
+-------+-----------------------+ 
| 1     | 2010-06-10   | 3      |
| 2     | 2010-06-11   | 2      |
| 3     | 2010-06-12   | 1      |
+-------+-----------------------+

What would be the query for this?

+5  A: 

This works if you can live without the id column in the result:

SELECT DATE(timestamp) AS type, COUNT(*) AS `count`
FROM sometable
GROUP BY DATE(timestamp)
ORDER BY DATE(timestamp)
Ignacio Vazquez-Abrams
Just wondering, when would you need a `DISTINCT`?
MvanGeest
Uh... not here...
Ignacio Vazquez-Abrams
@MvanGeest, you don't need a distinct here because you are doing GROUP BY!
VoodooChild
@VoodooChild: No, the reason you don't need DISTINCT here is because there are no joins.
Ignacio Vazquez-Abrams
@Ignacio Vazquez-Abrams: Well he could have done [SELECT DISTINCT (DATE(timestamp)) from sometable] INSTEAD OF [SELECT (DATE(timestamp) as t from sometable group by t], which would produce the same result. So in this sense, he would not need distinct when using group by!
VoodooChild
This is correct. You don't need order by DATE(timestamp) because group by will automatically order it anyway (in mysql at least). Except if you want another order, such as `order by 2 desc`, to get the most frequent days first.
ceteras
@VoodooChild : I was not replying to your discussion, I was pointing to the fact that Ignacio's answer is correct and made a further suggestion.
ceteras
A: 
SELECT
    DATE(timestamp),
    COUNT(*)
FROM
    My_Table
GROUP BY
    DATE(timestampe)

This doesn't give you a row number for each row. I don't know if (why?) that's important.

Tom H.
A: 
select date(timestamp) as type, count(*)
from your_table
group by type;
Ike Walker
I doubt if alias name can be directly used in the GROUP BY Clause
Madhivanan
@Madhivanan: MySQL supports the use of aliases from the SELECT clause in GROUP BY, ORDER BY, and HAVING clauses. From the manual: `A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses.`
Ike Walker
Ok. Thanks. It wont work in other RDBMSs
Madhivanan
A: 

This might work...

select DATE(timestamp), count(timestamp)
    from _table
group by timestamp
order by count(timestamp) desc
VoodooChild
A: 

SELECT count( DATE_FORMAT( timestamp, '%Y-%m-%d/' ) ) , DATE_FORMAT( timestamp, '%Y-%m-%d' ) FROM tablename group by DATE_FORMAT( timestamp, '%Y-%m-%d' );

You can not include the id in the select or the count will be off.

Spellfire