Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sql – total avg in groupby query

I want to find the avg salary for each day and in the same table I want to see the avg for all the week (total avg)

select weekday,avg(salary)
from table1
group by weekday

what I need to add in order to find the yellow cell?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can add the avg() regardless of weekday to your result with union all:

select weekday,avg(salary) from table1 group by weekday
union all
select max(0),avg(salary) from table1;

DBFiddle demo

Please next time pay attention to give some sample data. You can use Dbfiddle as I did.

BTW, when I started writing, I didn’t see SQL server tag. Now you have it, then this is the simpler one:

select weekday,avg(salary) from table1 group by weekday with rollup
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading