今天遇到群里發的一個問題,覺得有點意思,發上來看一下
以下代碼在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
)
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END