lijilin12 2014-12-15 12:55
浏览 332
已采纳

sql 条件查询case when

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条件哪里报缺失右括号的错误 请问错误是什么 怎样改正

  • 写回答

1条回答 默认 最新

  • weixin_42070001 2014-12-16 10:45
    关注

    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 ......
    没有办法测试,你自己测一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题