pbhle
pbhle

Reputation: 2936

get month wise total, week wise total and day wise total in one mysql query

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

Answers (2)

Pathik Vejani
Pathik Vejani

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

Abhishek Sharma
Abhishek Sharma

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

Related Questions