Reputation: 2936
I want to display month wise total, week wise total and then day wise total with days in one mysql query. I can do it using seperate queries but i want it in single query . Is it possible to display this hierarchy?
Upvotes: 1
Views: 400
Reputation: 4491
This is from link you have provided.
use UNION:
SELECT SUM(cost) AS total, MONTHNAME(date) AS month
FROM daily_expense
GROUP BY month
UNION
SELECT SUM(cost) AS total, CONCAT(date, ' - ', date + INTERVAL 6 DAY) AS week
FROM daily_expense
GROUP BY WEEK(date)
Upvotes: 0
Reputation: 6661
by using GROUP BY MONTH we found total by month and GROUP BY WEEK we found total by WEEK
Select sum(column) From table GROUP BY MONTH(column)
union
Select sum(column) From table GROUP BY WEEK(column)
and UNION operator combines the result
Upvotes: 1