2

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 Answers 5

5

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)
Sign up to request clarification or add additional context in comments.

5 Comments

@MvanGeest, you don't need a distinct here because you are doing GROUP BY!
@VoodooChild: No, the reason you don't need DISTINCT here is because there are no joins.
@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!
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.
@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.
0
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.

Comments

0
select date(timestamp) as type, count(*)
from your_table
group by type;

2 Comments

I doubt if alias name can be directly used in the GROUP BY Clause
@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.
0

This might work...

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.