I have a table which has data like this
id test_val type_cd
#-------------------------
101 TEST22 M
102 TEST23 M
103 TEST01 M
103 TEST01 H
104 TEST02 M
104 TEST02 H
105 TEST03 H
I would like to fetch the max(id) for each type_cd and its corresponding test_val in a single row output as below.
The expected output is:
M_id M_Test_Val H_id H_Test_Val
#-----------------------------------
104 TEST02 105 TEST03
If I have to fetch only the max(id) for each type_cd I'll have my query like this
select max(case when type_cd='M' then max(id) else null end) as M_id,
max(case when type_cd='H' then max(id) else null end) as H_id
from t1
group by type_cd;
I'm not sure how to get the test_val for the max(id)
for each type_cd
.