简体   繁体   中英

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?

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

这将达到目的:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM