Emery King
Emery King

Reputation: 3534

How to sum up grouped results in SQL

I'm trying to avoid involving php after this query. I have a table that holds a list of employees and how much time they worked in seconds,date, etc... i want to:

select SUM(`seconds`) between date A and date B group by WEEK(`date`)

that will give me results for each week but now i want to get an average seconds worked per week by using AVG() on the whole result set. How could you accomplish this in one query?

Upvotes: 0

Views: 161

Answers (2)

snowguy
snowguy

Reputation: 911

This will do the trick:

Select AVG(sum_seconds) from (select SUM('seconds') as sum_seconds between date A and date B group by WEEK('date')) as a

Upvotes: 1

Vinod Sharma
Vinod Sharma

Reputation: 146

You can use something like this

select sum(total) from (select SUM(`seconds`) as total between date A and date B group by WEEK(`date`)) as tbl1

Hope it help

Upvotes: 2

Related Questions