SQL: count days from table based on month
Clash Royale CLAN TAG#URR8PPP
SQL: count days from table based on month
Hello everyone,
I have this query on a mysql database:
SELECT DAYNAME(added_time) = 'Monday',
COUNT(CASE WHEN MONTH(added_time) = 1 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) mongen,
COUNT(CASE WHEN MONTH(added_time) = 2 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monfeb,
COUNT(CASE WHEN MONTH(added_time) = 3 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monmar,
COUNT(CASE WHEN MONTH(added_time) = 4 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monapr,
COUNT(CASE WHEN MONTH(added_time) = 5 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monmag,
COUNT(CASE WHEN MONTH(added_time) = 6 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) mongiu,
COUNT(CASE WHEN MONTH(added_time) = 7 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monlug,
COUNT(CASE WHEN MONTH(added_time) = 8 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monago,
COUNT(CASE WHEN MONTH(added_time) = 9 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monset,
COUNT(CASE WHEN MONTH(added_time) = 10 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monott,
COUNT(CASE WHEN MONTH(added_time) = 11 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) monnov,
COUNT(CASE WHEN MONTH(added_time) = 12 AND DAYNAME(added_time) = 'Monday' THEN 1 ELSE NULL END) mondic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Monday'
UNION
SELECT DAYNAME(added_time) = 'Tuesday',
COUNT(CASE WHEN MONTH(added_time) = 1 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuegen,
COUNT(CASE WHEN MONTH(added_time) = 2 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuefeb,
COUNT(CASE WHEN MONTH(added_time) = 3 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuemar,
COUNT(CASE WHEN MONTH(added_time) = 4 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueapr,
COUNT(CASE WHEN MONTH(added_time) = 5 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuemag,
COUNT(CASE WHEN MONTH(added_time) = 6 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuegiu,
COUNT(CASE WHEN MONTH(added_time) = 7 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuelug,
COUNT(CASE WHEN MONTH(added_time) = 8 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueago,
COUNT(CASE WHEN MONTH(added_time) = 9 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueset,
COUNT(CASE WHEN MONTH(added_time) = 10 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tueott,
COUNT(CASE WHEN MONTH(added_time) = 11 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuenov,
COUNT(CASE WHEN MONTH(added_time) = 12 AND DAYNAME(added_time) = 'Tuesday' THEN 1 ELSE NULL END) tuedic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Tuesday'
It returns this json:
["DAYNAME(added_time) = 'Monday'":"1","mongen":"0","monfeb":"5","monmar":"0","monapr":"2","monmag":"40","mongiu":"63","monlug":"96","monago":"10","monset":"0","monott":"0","monnov":"0","mondic":"0","DAYNAME(added_time) = 'Monday'":"1","mongen":"0","monfeb":"10","monmar":"3","monapr":"2","monmag":"38","mongiu":"59","monlug":"106","monago":"18","monset":"0","monott":"0","monnov":"0","mondic":"0"]
How to modify query so it will return a json like this?
["added_time = 'Monday'","mongen":"0","monfeb":"5","monmar":"0","monapr":"2","monmag":"40","mongiu":"63","monlug":"96","monago":"10","monset":"0","monott":"0","monnov":"0","mondic":"0","added_time = 'Tuesday'","tuegen":"0","tuefeb":"10","tuemar":"3","tueapr":"2","tuemag":"38","tuegiu":"59","tuelug":"106","tueago":"18","tueset":"0","tueott":"0","tuenov":"0","tuedic":"0"]
Thanks in advance,
Nico
Ok after rethinking the query I need this json:
["added_time":"Monday","gen":"0","feb":"5","mar":"0","apr":"2","mag":"40","giu":"63","lug":"96","ago":"10","aet":"0","ott":"0","nov":"0","dic":"0","added_time":"Tuesday","gen":"0","feb":"10","mar":"3","apr":"2","mag":"38","giu":"59","lug":"106","ago":"18","aet":"0","ott":"0","nov":"0","dic":"0"]
The wrong query I have right now is this:
SELECT added_time,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 1 THEN 1 ELSE NULL END) gen,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 2 THEN 1 ELSE NULL END) feb,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 3 THEN 1 ELSE NULL END) mar,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 4 THEN 1 ELSE NULL END) apr,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 5 THEN 1 ELSE NULL END) mag,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 6 THEN 1 ELSE NULL END) giu,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 7 THEN 1 ELSE NULL END) lug,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 8 THEN 1 ELSE NULL END) ago,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 9 THEN 1 ELSE NULL END) aet,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 10 THEN 1 ELSE NULL END) ott,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 11 THEN 1 ELSE NULL END) nov,
COUNT(CASE WHEN DAYNAME(added_time) = 'Monday' AND MONTH(added_time) = 12 THEN 1 ELSE NULL END) dic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Monday'
UNION
SELECT added_time,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 1 THEN 1 ELSE NULL END) gen,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 2 THEN 1 ELSE NULL END) feb,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 3 THEN 1 ELSE NULL END) mar,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 4 THEN 1 ELSE NULL END) apr,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 5 THEN 1 ELSE NULL END) mag,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 6 THEN 1 ELSE NULL END) giu,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 7 THEN 1 ELSE NULL END) lug,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 8 THEN 1 ELSE NULL END) ago,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 9 THEN 1 ELSE NULL END) aet,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 10 THEN 1 ELSE NULL END) ott,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 11 THEN 1 ELSE NULL END) nov,
COUNT(CASE WHEN DAYNAME(added_time) = 'Tuesday' AND MONTH(added_time) = 12 THEN 1 ELSE NULL END) dic
FROM shipping_details
WHERE DAYNAME(added_time) = 'Tuesday'
It returns this wrong json:
["added_time":"2018-02-12 09:23:29","gen":"0","feb":"5","mar":"0","apr":"2","mag":"40","giu":"63","lug":"96","ago":"10","aet":"0","ott":"0","nov":"0","dic":"0","added_time":"2018-02-13 09:32:07","gen":"0","feb":"10","mar":"3","apr":"2","mag":"38","giu":"59","lug":"106","ago":"18","aet":"0","ott":"0","nov":"0","dic":"0"]
How to modify the query so it will be added_time":"Monday"
and not added_time":"2018-02-12 09:23:29
?
added_time":"Monday"
added_time":"2018-02-12 09:23:29
Thanks for your help,
Nico
"added_time = 'Monday'"
Add some sample data and expected sql output of the query
– Joakim Danielson
Aug 12 at 19:03
1 Answer
1
Change this:
SELECT DAYNAME(added_time) = 'Monday',
to:
SELECT DAYNAME(added_time) as added_time,
Your version is added a boolean variable with no name -- hence the name is defaulted to the expression and the value is 0/1. You want to name the column, so use as
. I would call it something like day_of_week
, but you seem to prefer added_time
.
as
day_of_week
added_time
I would suggest that you change the entire query to:
SELECT DAYNAME(added_time) as added_time,
SUM(MONTH(added_time) = 1) as mongen,
SUM(MONTH(added_time) = 2) as monfeb,
. . .
FROM shipping_details
GROUP BY DAYNAME(added_time)
A single GROUP BY
seems much simpler than a UNION ALL
. The use of SUM()
with a boolean expression is a MySQL extension that is quite convenient (and makes sense in the context of other programming languages).
GROUP BY
UNION ALL
SUM()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That's not valid JSON, the first entry (
"added_time = 'Monday'"
) has no value associated with it.– Nick
Aug 12 at 13:13