Mittensonmaui
asked on
SQL Server Group By having issue
I have the following
SELECT PCNS_Staff_NO, sum(PCNS_STAFF_SR_ALLOCATI ON_TOTAL_H OURS)/
IsNull(NullIf(sum(DateDiff (dd,PCNS_S TAFF_SR_AL LOCATION_S TART_DATE, PCNS_STAFF _SR_ALLOCA TION_END_D ATE))), 0), 1)
FROM PCNS_STAFF_SR_ALLOCATION
Group by PCNS_Staff_NO, PCNS_STAFF_SR_ALLOCATION.P CNS_STAFF_ SR_ALLOCAT ION_END_DA TE
having PCNS_STAFF_SR_ALLOCATION_E ND_DATE > '2007/01/01'
order by PCNS_STAFF_NO
I want to group by PCNS_Staff_NO only, but the having clause won't allow it because of the END_DATE field
SELECT PCNS_Staff_NO, sum(PCNS_STAFF_SR_ALLOCATI
IsNull(NullIf(sum(DateDiff
FROM PCNS_STAFF_SR_ALLOCATION
Group by PCNS_Staff_NO, PCNS_STAFF_SR_ALLOCATION.P
having PCNS_STAFF_SR_ALLOCATION_E
order by PCNS_STAFF_NO
I want to group by PCNS_Staff_NO only, but the having clause won't allow it because of the END_DATE field
what issue are you having?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hmm interesting, I always thought you needed to use having with group by, but it works
>> I always thought you needed to use having with group by, but it works <<
That depends on what type of check you are doing. It's possible you might need both WHERE and HAVING, or either alone.
Btw, the two statements above could yield different results: they are *not* equivalent. They are only the same if every unique PCNS_Staff_NO has either 1) *no* dates after the desired date or 2) *all* dates
after the desired date. If the dates for the same Staff_NO are mixed, before and after the date, you should see a different result from the two queries.
Frankly I think the first one is not at all logical but I suppose in theory it could be what you want (?).
That depends on what type of check you are doing. It's possible you might need both WHERE and HAVING, or either alone.
Btw, the two statements above could yield different results: they are *not* equivalent. They are only the same if every unique PCNS_Staff_NO has either 1) *no* dates after the desired date or 2) *all* dates
after the desired date. If the dates for the same Staff_NO are mixed, before and after the date, you should see a different result from the two queries.
Frankly I think the first one is not at all logical but I suppose in theory it could be what you want (?).