【问题标题】:Case statement errors案例陈述错误
【发布时间】:2017-08-09 09:14:12
【问题描述】:

当在第 3 行时,我知道我在 case 语句中做错了什么,但无法弄清楚是什么

select DISTINCT p.code, a.itemno, a.descrip, a.rev, a.class, a.std_cost, a.std_price, t.trans_in_out, t.trans_quan,
    (select case when V.MIN_CUST_PRICE is null then
    when V.MIN_CUST_NAT_PRICE is null then
    when V.MIN_SELL_PRICE is null then 0
    else V.MIN_SELL_PRICE
    else V.MIN_CUST_NAT_PRICE
    else V.MIN_CUST_PRICE
    end
    from v_sca_item_price_summary v where rownum = '1') as Value
    from arinvt a, translog t, eplant e, v_sca_item_price_summary v, prod_code p
    where v.arinvt_id = a.id
    and a.prod_code_id = p.id
    and t.eplant_id = e.id
    and t.trans_date between :start_date and :end_date

【问题讨论】:

  • 选择当 V.MIN_CUST_PRICE 为空时的情况(然后呢?)
  • 我想说的是,如果以下任何一个 V.MIN_CUST_NAT_PRICE V.MIN_SELL_PRICE V.MIN_SELL_PRICE 为空,那么具有值的那个应该是最终结果

标签: sql oracle case


【解决方案1】:

将整个case when... 更改为coalesce()

coalesce(V.MIN_CUST_PRICE, V.MIN_CUST_NAT_PRICE, V.MIN_SELL_PRICE, 0)

【讨论】:

    【解决方案2】:

    根据Oracle nested CASE statements应该是这样的:

    select DISTINCT p.code, a.itemno, a.descrip, a.rev, a.class, a.std_cost, a.std_price, t.trans_in_out, t.trans_quan,
        (select case when V.MIN_CUST_PRICE is null then
        (case when V.MIN_CUST_NAT_PRICE is null then
        (case when V.MIN_SELL_PRICE is null then 0
        else V.MIN_SELL_PRICE end)
        else V.MIN_CUST_NAT_PRICE end)
        else V.MIN_CUST_PRICE
        end
        from v_sca_item_price_summary v where rownum = '1') as Value
        from arinvt a, translog t, eplant e, v_sca_item_price_summary v, prod_code p
        where v.arinvt_id = a.id
        and a.prod_code_id = p.id
        and t.eplant_id = e.id
        and t.trans_date between :start_date and :end_date
    

    希望对你有所帮助:)

    【讨论】:

    • 嘿,它工作并运行,但唯一的问题是它只返回 0 并且没有获取替换 0 的值
    • 我猜测V.MIN_CUST_PRICEV.MIN_CUST_NAT_PRICEV.MIN_SELL_PRICE 的值为空。检查并告诉我结果。
    【解决方案3】:

    您的CASE 语法错误...实际语法是

    CASE WHEN <condition> THEN VALUE
    WHEN <condition> THEN VALUE
    ...
    ELSE default_value END
    

    将其与您的比较,您会发现缺少 THEN 值并且您有多个 ELSE 语句的错误

    case when V.MIN_CUST_PRICE is null then ???
        when V.MIN_CUST_NAT_PRICE is null then ???
        when V.MIN_SELL_PRICE is null then 0
        else V.MIN_SELL_PRICE ???
        else V.MIN_CUST_NAT_PRICE ???
        else V.MIN_CUST_PRICE ???
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2010-10-30
      • 2016-05-13
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      相关资源
      最近更新 更多