0 0

sql 条件查询case when5

select *
  from (case when(t1.bxdw = t2.bxdw) then
         select t1.bxdw bxdw,
                (t1.zje - t2.zje) pk,
                t1.tpfy tpfy,
                (t1.yx - t2.tpsm) yx,
                t2.tpsm tpsm,
                (t1.zje - t2.zje + t1.tpfy) zje else
                 select t1.bxdw bxdw,
                        t1.zje pk,
                        t1.tpfy tpfy,
                        t1.yx yx,
                        (t1.yx - t1.yx) tpsm,
                        (t1.zje + t1.tpfy) zje end
                   from (select bxdw,
                                sum(zjje) zje,
                                count(*) yx,
                                sum(tpfy) tpfy
                           from CWXT_JPDG_B_YDXX
                          group by bxdw) t1,
                        (select bxdw, sum(zjje) zje, count(*) tpsm
                           from CWXT_JPDG_B_YDXX
                          where sftp = 1
                          group by bxdw) t2
                
                 )
在when条件哪里报缺失右括号的错误  请问错误是什么  怎样改正
 
2014年12月15日 12:55

1个答案 按时间排序 按投票排序

0 0

采纳的答案

case when不是这么用的。。

SELECT (case when cl.colno=cc.colno then cl.cola||cc.colb else cl.colb||cc.colc end) as testa,
(case when cl.colno=cc.colno then cl.sortno||cc.colc else cl.colf||cc.colg end) as testb  FROM  tabcl cl,tabcc cc

按你的sql大概可以改成这样
select t1.bxdw,(case when t1.bxdw = t2.bxdw then (t1.zje - t2.zje) then t1.zje end) as pk,t1.tpfy,
(case when t1.bxdw = t2.bxdw then (t1.yx - t2.tpsm) else t1.yx end) as yx,
(case when t1.bxdw = t2.bxdw then t2.tpsm else (t1.yx - t1.yx) end) as tpsm,
(case when t1.bxdw = t2.bxdw then (t1.zje - t2.zje + t1.tpfy) else (t1.zje + t1.tpfy) end) as zje from ......
没有办法测试,你自己测一下。

2014年12月16日 10:45

相关推荐

Global site tag (gtag.js) - Google Analytics