从外部选择使用案例时

时间:2017-05-12 15:27:52

标签: sql

我使用外部申请创建查找两个结果,但我只需要使用一个取决于他们返回的内容。我在select中使用Case测试行。但它没有按预期工作。

This Can
Be
Multi line!

所以'输出'列应该始终有一个订单号。

结果

select (case when s.ord_hdrnumber = '0' then (case when s2.ord_hdrnumber = NULL then s3.ord_hdrnumber else s2.ord_hdrnumber end) else s.ord_hdrnumber end) ord, 
into #stops2
from #Stops s left join assetassignment a
    on s.lgh_number = a.lgh_number
    outer apply (select top 1 * from #stops where #stops.mov_number = s.mov_number and #stops.stp_sequence > s.stp_sequence and #stops.ord_hdrnumber > 0 order by #stops.stp_sequence asc) s2
    outer apply (select top 1 * from #stops where #stops.mov_number = s.mov_number and #stops.stp_sequence < s.stp_sequence and #stops.ord_hdrnumber > 0 order by #stops.stp_sequence desc) s3

我做错了什么?

0 个答案:

没有答案