1

I want to query these below data sample. (Oracle database)

table name: FL_TEST

NAMA    JAM  STATUS
A       2    P
A       1    O
A       3    O
B       1    O
B       2    P
B       3    O
C       1    O
C       2    P
C       3    P

to be like this:

NAMA    JAM1 JAM2 JAM3
A       O    P    O
B       O    P    O
C       O    P    P

Is it possible to do that query?

HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

2 Answers2

3

use conditional aggregaiton

select NAMA ,max(case when jam=1 then status end) as jam1,
max(case when jam=2 then status end) as jam2,
max(case when jam=3 then status end) as jam3
from FL_TEST group by NAMA

or you can use pivot

SELECT *
  FROM FL_TEST
PIVOT (
  MAX(status) as JAM FOR jam IN (1,2,3)
 );
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
2

You can use Conditional Aggregation :

select nama, 
       max( case when jam = 1 then status end ) as jam1,
       max( case when jam = 2 then status end ) as jam2,
       max( case when jam = 3 then status end ) as jam3
  from tab
 group by nama

or pivot :

select * 
  from tab
 pivot(
       max(value) for field in ( 1 as "Jam1", 
                                 2 as "Jam2",
                                 3 as "Jam3" ) )

Demo

Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55