包含多个 SELECT 语句的案例

CASE WITH MULTIPLE SELECT STATEMENTS

我正在尝试将 select 语句放入案例中,但无法正确放置。

第一个case语句没问题,第二个就出问题了

有人可以用正确的语法指导我吗,

select u.unit_id,
       u.order_code,
       u.order_item,
       u.roll_sheet,
       u.date_wound,
       u.date_shipped,
       u.unit_status,
       u.warehouse_code,
       (case
         when u.unit_id_turnup is not null then
          (select u1.onput_id
             from unit u1
            where u.unit_id_turnup = u1.unit_id)
         else
          u.onput_id
       end) "onput id",
       
       (case
         when u.unit_id_turnup is not null then
          (select CASE WHEN U1.UNIT_ID_TURNUP IS NOT NULL
             from unit u1 then (select u3.onput_id
                                  from unit u3
                                 where u1.unit_id_turnup = u3.unit_id) else u1.onput_id)
         else
          u.onput_id
       end) "onput id2",
  from unit u

我可以推测您想要的代码是:

select . . . ,
       coalesce(u1.onput_id, u.onput_id) as onput_id,
       coalesce(u2.onput_id, u1.onput_id, u.onput_id) as onput_id2
from unit u left join
     unit u1
     on u.unit_id_turnup = u1.id left join
     unit u2
     on u1.unit_id_turnup = u2.id

没有样本数据和明确的解释,这真的只是一个猜测。但是,这似乎是您正在尝试的。