按顺序更改列数据位置

时间:2019-11-12 05:54:40

标签: oracle sql-order-by

tbl

SEQ1 | SEQ2 | D1                  | D2       
---: | ---: | :------------------ | :--------
   1 |    1 | 12-SEP-19 4:10:00 PM| 12-SEP-19 4:14:00 PM
   2 |    2 | 12-SEP-19 4:20:00 PM| 12-SEP-19 4:36:00 PM
   3 |    3 | 12-SEP-19 4:30:00 PM| 12-SEP-19 4:52:00 PM
   4 |    4 | 12-SEP-19 4:35:00 PM| 12-SEP-19 5:16:00 PM
   5 | null | 12-SEP-19 4:40:00 PM| null
   6 | null | 12-SEP-19 4:45:00 PM| null     
   7 | null | 12-SEP-19 4:50:00 PM| null    
   8 | null | 12-SEP-19 5:00:00 PM| null      
   9 | null | 12-SEP-19 5:10:00 PM| null      
   10| null | 12-SEP-19 5:20:00 PM| null      

我想基于这样的d1数据更改订单d2

SEQ1 | SEQ2 | D1                  | D2       
---: | ---: | :------------------ | :--------
   1 |    1 | 12-SEP-19 4:10:00 PM| 12-SEP-19 4:14:00 PM
   2 | null | 12-SEP-19 4:20:00 PM| null
   3 | null | 12-SEP-19 4:30:00 PM| null
   4 | null | 12-SEP-19 4:35:00 PM| null
   5 |    2 | 12-SEP-19 4:40:00 PM| 12-SEP-19 4:36:00 PM
   6 | null | 12-SEP-19 4:45:00 PM| null     
   7 | null | 12-SEP-19 4:50:00 PM| null    
   8 |    3 | 12-SEP-19 5:00:00 PM| 12-SEP-19 4:52:00 PM  
   9 | null | 12-SEP-19 5:10:00 PM| null      
   10|    4 | 12-SEP-19 5:20:00 PM| 12-SEP-19 5:16:00 PM     

如何订购表和更改头寸行数据? 请帮助我...

db <>提琴here

1 个答案:

答案 0 :(得分:1)

使用以下排序逻辑:

ORDER BY COALESCE(D2, D1)

您更新的查询:

SELECT
    SEQ1,
    SEQ2,
    D1,
    D1
FROM yourTable
ORDER BY
    COALESCE(D2, D1);

Demo

(已更新)