Skip to content
Advertisement

SQL – Naming conditions for filtering

I wrote MySQL query to filter and count items in a table by date and now a need to get results like:

+-----+--------------------+
| cnt | creation_date_date |
+-----+--------------------+
| 90  | 2019-11-15         |
| 31  | 2019-11-16         |
| 9   | 2019-11-17         |
| 80  | 2019-11-18         |
| 40  | 2019-11-19         |
| 32  | 2019-11-20         |
| 132 | 2019-11-21         |
+-----+--------------------+

and i got:

+-----+
| cnt |
+-----+
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
+-----+

I don’t know where to declare name creation_date_date in mine condition?

My query:

SELECT COUNT(*) as cnt
FROM wallet
WHERE created >= CAST('2019-11-15' AS DATE) 
AND created <= CAST('2019-12-06' AS DATE)
GROUP BY created

Advertisement

Answer

From your output we can only guess. You forgot to display the created column with the counts, so we cannot tell what the count refers to. Maybe your created column is a datetime and you only want the date?

SELECT date(created) as date_created, COUNT(*) AS cnt
FROM wallet
WHERE created >= date '2019-11-15' AND created < date '2019-12-07'
GROUP BY date(created)
ORDER BY date(created);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement