我是Oracle SQL新手。我需要一个SQL查询,可以转换下面提到的表。我尝试了很少的选项,如unpivot,union等,但我仍然无法实现。
我有一张这样的表:
\output decl -> fmap (output <>) (codegenDecl decl)
我需要按以下方式获取
SNO Name Sales Profit
1 John 50 20
2 Peter 60 NULL
3 Mark 15 10
4 Nicolas NULL -10
5 Alex 70 20
另外,如果还有其他方法可以解决这个问题,我会很感激。
答案 0 :(得分:0)
一种简单的方法使用union all
:
select SNO, Name, Sales, NULL as Profit, 'Sales' as sales_pro
from t
where profit is not null
union all
select SNO, Name, NULL as Sales, Profit, 'Profit' as sales_pro
from t
where sales is not null;
答案 1 :(得分:0)
select
SNO, Name, Sales, Profit,
case
when Sales is not null then 'Sales'
when Profit is not null then 'Profit'
else null
end as sales_pro
from t
答案 2 :(得分:0)
有效的方法(只从基表读取一次数据)使用交叉连接到您可以动态创建的小帮助程序表 - 作为内联视图,在查询的FROM子句中。
注意顶部的WITH子句 - 它只是允许我测试查询。它不是解决方案的一部分;删除它,并使用您的实际表和列名称。
with
test_data ( sno, name, sales, profit ) as (
select 1, 'John' , 50, 20 from dual union all
select 2, 'Peter' , 60, NULL from dual union all
select 3, 'Mark' , 15, 10 from dual union all
select 4, 'Nicolas', NULL, -10 from dual union all
select 5, 'Alex' , 70, 20 from dual
)
select sno, name,
case sal_pro when 'Sales' then sales end as sales,
case sal_pro when 'Profit' then profit end as profit, sal_pro
from test_data
cross join
(select 'Sales' as sal_pro from dual union all select 'Profit' from dual)
where case sal_pro when 'Sales' then sales else profit end is not null
order by sno, sales
;
SNO NAME SALES PROFIT SAL_PR
---------- ------- ---------- ---------- ------
1 John 50 Sales
1 John 20 Profit
2 Peter 60 Sales
3 Mark 15 Sales
3 Mark 10 Profit
4 Nicolas -10 Profit
5 Alex 70 Sales
5 Alex 20 Profit