用于连接不同表的SQL

时间:2016-10-03 16:40:32

标签: sql oracle select join

我有以下表格:

enter image description here

enter image description here

我想编写一个显示以下结果的选择状态

enter image description here

我试图加入/ Union / Union所有这两个表但是我没有得到deird输出。任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

select
 'group_a' as groupname
, max( case classname when 'C123' then group_a else null end)  as c123
, max( case classname when 'C456' then group_a else null end)  as c456
from table1
union all
select
 'group_b' as groupname
, max( case classname when 'C123' then group_b else null end)  as c123
, max( case classname when 'C456' then group_b else null end)  as c456
from table1
union all
select
 'group_d' as groupname
, max( case classname when 'C123' then group_d else null end)  as c123
, max( case classname when 'C456' then group_d else null end)  as c456
from table1
union all
select
 'group_e' as groupname
, max( case classname when 'C123' then group_e else null end)  as c123
, max( case classname when 'C456' then group_e else null end)  as c456
from table1