简体   繁体   中英

Oracle SQL Converting Rows Into Column for similar fields

I have a table with two fields

EMPLID | STDNT_GROUP
A1       H025
A1       M050
A1       T100
A2       H070
A2       M075
A3       H100
A3       T025

Output

Emplid |  Hos  | Tut | Mess
A1       H025    T100  M050
A2       H070    NULL  M075
A3       H100    T025  NULL

I want to convert the rows into columns. If an emplid has group starting with H then Hos,with T then Tut and if M then Mess. Is this possible using sql

Select emplid, max(case when substr(STDNT_GROUP,1) like 'H%' then STDNT_GROUP else ' ' end ) Hostel, 
max(case when substr(STDNT_GROUP,1) like 'T%' then STDNT_GROUP else ' '  end ) Tuition, 
max(case when substr(STDNT_GROUP,1) like 'M%' then STDNT_GROUP  else ' ' end ) Mess 
from table a
group by emplid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM