看看这条语句错在哪里?

tianlei521 2005-11-30 09:23:08
select
min(case when jysj>='2005/11/01' and jysj<='2005/11/30' then lcb end) as sqlc,
max(case when jysj>='2005/11/01' and jysj<='2005/11/30' then lcb end) as bqlc,
sum(case when jysj>='2005/11/01' and jysj<='2005/11/30' then convert(numeric(20,2),lcb)
when jysj>='2005/11/01' and jysj<='2005/11/30' then -convert(numeric(20,2),lcb)
end) as bqxs,
sum(case when jysj>='2005/11/01' and jysj<='2005/11/30' then convert(numeric(20,2),jyl)
when jysj>='2005/11/01' and jysj<='2005/11/30' then -convert(numeric(20,2),jyl)
end) as bqyy,
a.cldm
from
car_jyxx a
where
exists(select 1 from car_clxx where cldm=a.cldm)
group by
a.cldm

执行后得到的bqxs的值应该是bqlc减去sqlc的值,可是怎么得到的是两个相加的值呢?帮忙啊!
...全文
131 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianlei521 2005-11-30
  • 打赏
  • 举报
回复
子陌红尘老兄,要怎样才能得到我要的结果呢?
wgsasd311 2005-11-30
  • 打赏
  • 举报
回复
select
min(case when jysj>='2005/11/01' and jysj<='2005/11/30' then lcb end) as sqlc,
max(case when jysj>='2005/11/01' and jysj<='2005/11/30' then lcb end) as bqlc,

( max(case when jysj>='2005/11/01' and jysj<='2005/11/30' then convert(numeric(20,2),lcb)
else 0 end)-
min(case when jysj>='2005/11/01' and jysj<='2005/11/30' then -convert(numeric(20,2),lcb)
else 0 end) )as bqxs,
sum(case when jysj>='2005/11/01' and jysj<='2005/11/30' then convert(numeric(20,2),jyl)
when jysj>='2005/11/01' and jysj<='2005/11/30' then -convert(numeric(20,2),jyl)
end) as bqyy,
a.cldm
from
car_jyxx a
where
exists(select 1 from car_clxx where cldm=a.cldm)
group by
a.cldm
子陌红尘 2005-11-30
  • 打赏
  • 举报
回复
看仔细,两个when之后的条件语句是相同的,因此只执行第一个when后的then

sum(case
when jysj>='2005/11/01' and jysj<='2005/11/30' then convert(numeric(20,2),lcb)
when jysj>='2005/11/01' and jysj<='2005/11/30' then -convert(numeric(20,2),lcb)
end) as bqxs,

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧