× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Any field you select must be in the group by clause, however you can group by on fields that are not selected. With that being said, in the selection of fields, change PLBPTP to match your case statement in the group by.

SELECT eeun, eedpt, plbemp, eenam, plbbg1, case PLBPTP when 'CO' then 'CALLOUT'

Else 'OT'
End, sum(plbhrs)
FROM PRLBH, EEMP
WHERE PLBEMP = EEENO and PLBPTP in('CO','OH', '6D','7D','OG')

and EETDT = 0 and EEDPT in (select PRDPT from PROVRDPT00 where
PRNAME = 'BRANDTNS')

and PLBEDT = 20100228

GROUP BY PLBEMP,EENAM,PLBBG1,EEUN, EEDPT ,
case PLBPTP when 'CO' then 'CALLOUT'

Else 'OT'
End

-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Smith, Mike
Sent: Friday, November 19, 2010 4:04 PM
To: Midrange Systems Technical Discussion
Subject: Problems with Group by and case in sql

I have a SQL I'm trying to write. My first attempt is trying to use a
case statement in a GROUP BY clause.

SELECT eeun, eedpt, plbemp, eenam, plbbg1, plbptp, sum(plbhrs)
FROM PRLBH, EEMP
WHERE PLBEMP = EEENO and PLBPTP in('CO','OH', '6D','7D','OG')

and EETDT = 0 and EEDPT in (select PRDPT from PROVRDPT00 where
PRNAME = 'BRANDTNS')

and PLBEDT = 20100228

GROUP BY PLBEMP,EENAM,PLBBG1,EEUN, EEDPT ,
case PLBPTP when 'CO' then 'CALLOUT'

Else 'OT'
End

I get errors telling me PLBPTP or expression in select list not valid

If I remove the case on the group by I get results, but not what I would
like ideally.

In researching this, I came across a CTE example that I worked like this


with x (a,b,c,d,e,f) as (SELECT eeun, eedpt, plbemp, eenam, plbbg1,
sum(plbhrs) case plbptp when 'CO' then 'CALLOUT'
when 'OT' then 'OVERTIME'
when '6D' then 'OVERTIME'
when '7D' then 'OVERTIME'
when 'OG' then 'OVERTIME'
else 'OTHERS'
end
from PRLBH, EEMP
WHERE PLBEMP = EEENO
and PLBPTP in('CO','OH', '6D','7D','OG') and EETDT = 0 and EEDPT
in (select PRDPT from PROVRDPT00 where PRNAME = 'BRANDTNS') and
PLBEDT = 20100228)
select a,b,c,d,e,f from x group by c,d,e,a,b,f


On this one I get token PLBPTP not valid on my case statement.

This one will work if I remove the sum(plbhrs), but I need it in my
final results.

Any idea what I'm doing wrong on either one of these.

Thanks for any help

Mike



NOTICE: This message, including any attachment, is intended as a confidential and privileged communication. If you have received this message in error, or are not the named recipient(s), please immediately notify the sender and delete this message.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.