-2

Below is my EMP table with some sample data

EMPNAME | WORK_DATA | WORK_HOURS
---------------------------------
abc     | 01-SEP-13 |     9
xyz     | 01-SEP-13 |     8
abc     | 02-SEP-13 |     8
xyz     | 02-SEP-13 |     7
abc     | 01-OCT-13 |     5
xyz     | 01-OCT-13 |     7
abc     | 02-OCT-13 |     8
xyz     | 02-OCT-13 |     7

And now I want this to be displayed as in oracle using select statement

EMPNAME | SEP_MONTH_WORKING_HOURS | OCT_MONTH_WORKING_HOURS 
-----------------------------------------------------------
abc     | 17                      | 13
xyz     | 15                      | 14

Seond doubt is:

EMPNAME | SEP_MONTH_WORKING_HOURS | RANK 
-----------------------------------------------------------
xyz     | 15                      | 1
abc     | 17                      | 2

1 Answers1

2

This should work using extract:

select empname, 
  sum(case when  extract(month from work_data) = 9 then work_hours end) sept_work_hours,
  sum(case when  extract(month from work_data) = 10 then work_hours end) oct_work_hours
from emp
group by empname

Please note, abcs employee for september should be 17 instead of 19.

sgeddes
  • 62,311
  • 6
  • 61
  • 83