|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Compare Aggregates
I have two SQL queries which work one provides the avg number of sales by department and one which shows the total sales by dept. I am trying to combine the queries to show the deptartments that sold less than the average:
SELECT deptname, SUM(saleqty) FROM qsale WHERE saleqty < (SELECT SUM(saleqty) / COUNT (DISTINCT deptname) FROM qsale) GROUP BY deptname I'm trying to understand how to do this comparison. The saleqty in my WHERE clause does not reflect the SUM(saleqty). How should this be formatted? |
|
#2
|
||||
|
||||
|
untested:
Code:
select deptname, sum(saleqty)
from qsale
group
by deptname
having sum(saleqty)
< ( select sum(saleqty)
/ count(distinct deptname)
from qsale )
rudy http://r937.com/ |
|
#3
|
|||
|
|||
|
Thanks Rudy, it worked fine. I did not think to place the sub select in the having clause...
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Compare Aggregates |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|