Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with plsql (case statement)

Status
Not open for further replies.

bachuss

IS-IT--Management
Jul 17, 2002
1
US
I use case statements all the time in TSQL.
But my new job is working with PL/SQL SQL+.
I can not figure out how to do this in PL/SQL SQL+.
HELP
Example: (Tale in sql server)

TimecardTbl

Empid period hours
100 1 40
200 1 35
100 2 20
200 2 10

(my Tsql statement)

select empid,
P1Hrs = case when period = ‘1’ then sum(hours)
Else 0
End
P2Hrs = case when period = ‘2’ then sum(hours)
Else 0
End
from timecardtbl
group by empid, period

Results:

Empid P1Hrs P2Hrs
100 40 20
200 35 10

Please help
 
Have you tried this ?
select empid,
case when period = '1' then sum(hours)
Else 0
End AS P1Hrs,
case when period = '2' then sum(hours)
Else 0
End AS P2Hrs
from timecardtbl
group by empid, period

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I would actually do it like this in T-SQL:

Code:
SELECT empid,
  SUM(CASE WHEN period = 1 THEN hours ELSE 0 END) AS p1hours,
  SUM(CASE WHEN period = 2 THEN hours ELSE 0 END) AS p2hours
FROM timecardtbl
GROUP BY empid

Does this work in PL-SQL?

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top