以下代碼在MYSQL中測試通過,MSSQL應該能跑通,未測試。
復制代碼 代碼如下:
#創建表如下
create temporary table tmp (a int, b int ,c int);
insert into tmp VALUES (1,10,1),(10,10,2),(10,100,2);
#mysql執行
select sum(case when c = '1' then A else B end) from tmp
#oracle執行
select sum(decode(c,'1',a,b)) from tmp
#普通聯合查詢
select sum(d) from
(
select a as d from tmp where c=1
union
select b as d from tmp where c=2
)