qi1128 2022-01-19 09:06 采纳率: 100%
浏览 32
已结题

case when 语句 为什么第一句无论正确与否 都会跟随第二语句判断

数据库:MySQL
表结构:

img

img

img


我的代码:
select month(xl.zTime) '月份',
(case when((select sum(cp.pricexl.SALE_AMOUNT)from 销量明细 xl
left join 客户明细 kh on xl.customId = kh.customId
left join 产品明细 cp on xl.pid = cp.pid
where kh.Country = 'China' and month(xl.zTime) = 08) > 10000) then '合格'
when ((select sum(cp.price
xl.SALE_AMOUNT)from 销量明细 xl
left join 客户明细 kh on xl.customId = kh.customId
left join 产品明细 cp on xl.pid = cp.pid
where kh.Country = 'China' and month(xl.zTime) = 09) > 12000)then '合格'
else '不合格' end)
as '中国业绩情况',
(case when((select sum(cp.pricexl.SALE_AMOUNT)from 销量明细 xl
left join 客户明细 kh on xl.customId = kh.customId
left join 产品明细 cp on xl.pid = cp.pid
where kh.Country = 'England' and month(xl.zTime) = 08) > 10000)then '合格'
when((select sum(cp.price
xl.SALE_AMOUNT)from 销量明细 xl
left join 客户明细 kh on xl.customId = kh.customId
left join 产品明细 cp on xl.pid = cp.pid
where kh.Country = 'England' and month(xl.zTime) = 09) > 12000)then '合格'
else '不合格' end)
as '英国业绩情况'
from 销量明细 xl
left join 客户明细 kh on xl.customId = kh.customId
left join 产品明细 cp on xl.pid = cp.pid
group by month(xl.zTime)

输出结果:

img

  • 写回答

3条回答 默认 最新

  • DarkAthena ORACLE应用及数据库设计方案咨询师 2022-01-19 15:01
    关注

    这sql让人血压升高。。。case when 哪有你这样用的,我按你这个逻辑改了下

    select mon,
    case 
    when Country = 'China' and mon = 8 and total>10000 then '合格' 
    when kh.Country = 'China' and mon = 9 and total>12000  then '合格' 
    else '不合格' end 中国业绩情况,
    case 
    when Country = 'England' and mon = 8 and total>10000 then '合格' 
    when Country = 'England' and mon = 9 and total>12000  then '合格' 
    else '不合格' end 英国业绩情况
    from (
    select month(xl.zTime) mon,kh.Country,sum(cp.price*xl.SALE_AMOUNT) total
    from 产品明细 cp,销量明细 xl,客户明细 kh 
    where xl.pid = cp.pid 
    and xl.customId = kh.customId 
    and month(xl.zTime) in (8,9) 
    and Country in ('England','China')
    group by month(xl.zTime),kh.Country
    );
    

    你可以先把中间这个子查询弄出来,看看有没有数据


    稍微改了下,以后测试数据请提供建表sql及insert语句,要不然回答问题的人还得替你去造数据测试sql

    select mon,
    max(case 
    when kh.Country = 'China' and mon = 8 and total>10000 then '合格' 
    when kh.Country = 'China' and mon = 9 and total>12000  then '合格' 
    when kh.Country = 'China' and mon in (8, 9) then '不合格'
     end) 中国业绩情况,
    max(case 
    when kh.Country = 'England' and mon = 8 and total>10000 then '合格' 
    when kh.Country = 'England' and mon = 9 and total>12000  then '合格' 
    when kh.Country = 'England' and mon in (8, 9) then '不合格'
     end) 英国业绩情况
    from (
    select month(xl.zTime) mon,kh.Country,sum(cp.price*xl.SALE_AMOUNT) total
    from 产品明细 cp,销量明细 xl,客户明细 kh 
    where xl.pid = cp.pid 
    and xl.customId = kh.customId 
    and month(xl.zTime) in (8,9) 
    and Country in ('England','China')
    group by month(xl.zTime),kh.Country
    ) group by mon;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月28日
  • 已采纳回答 1月20日
  • 创建了问题 1月19日

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法