SQL Server Group By having issue

I have the following

SELECT     PCNS_Staff_NO, sum(PCNS_STAFF_SR_ALLOCATION_TOTAL_HOURS)/
           IsNull(NullIf(sum(DateDiff(dd,PCNS_STAFF_SR_ALLOCATION_START_DATE,PCNS_STAFF_SR_ALLOCATION_END_DATE))), 0), 1)
FROM         PCNS_STAFF_SR_ALLOCATION
Group by PCNS_Staff_NO, PCNS_STAFF_SR_ALLOCATION.PCNS_STAFF_SR_ALLOCATION_END_DATE
having PCNS_STAFF_SR_ALLOCATION_END_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
MittensonmauiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
what issue are you having?
0
chapmandewCommented:
try this:
SELECT     PCNS_Staff_NO, sum(PCNS_STAFF_SR_ALLOCATION_TOTAL_HOURS)/
           IsNull(NullIf(sum(DateDiff(dd,MAX(PCNS_STAFF_SR_ALLOCATION_START_DATE),MAX(PCNS_STAFF_SR_ALLOCATION_END_DATE)))), 0), 1)
FROM         PCNS_STAFF_SR_ALLOCATION
Group by PCNS_Staff_NO
having MAX(PCNS_STAFF_SR_ALLOCATION_END_DATE) > '2007/01/01'
order by PCNS_STAFF_NO

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
You need WHERE not HAVING:

SELECT     PCNS_Staff_NO, sum(PCNS_STAFF_SR_ALLOCATION_TOTAL_HOURS)/
           IsNull(NullIf(sum(DateDiff(dd,PCNS_STAFF_SR_ALLOCATION_START_DATE,PCNS_STAFF_SR_ALLOCATION_END_DATE)), 0), 1)
FROM         PCNS_STAFF_SR_ALLOCATION
where PCNS_STAFF_SR_ALLOCATION_END_DATE > '20070101'
Group by PCNS_Staff_NO
order by PCNS_STAFF_NO
0
MittensonmauiAuthor Commented:
Hmm interesting, I always thought you needed to use having with group by, but it works
0
Scott PletcherSenior DBACommented:
>> 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 (?).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.