繁体   English   中英

有没有办法在 oracle SQL 的一行中打印列的先前值和更改值

[英]Is there any way to print previous value of column and changed values in one row in oracle SQL

SELECT  product_id , module_no , product_status ,
  LAG(module_no) OVER (PARTITION BY product_id ORDER BY module_no) AS OLD_V
FROM    product
WHERE   product_id  = '1'
ORDER BY
        module_no;

需要结果作为第二张表

在此处输入图像描述

您拥有基本查询,并且很少使用主要用case来实现您想要的操作。

select product_id
      ,case when current_module_no != prev_module_no
            then prev_module_no
       end module_no_old
      ,current_module_no module_no_new
      ,case when current_product_status != prev_product_status
            then prev_product_status
       end product_status_old
      ,current_product_status product_status_new
from
(
select  product_id
      , module_no current_module_no
      , product_status current_product_status
      , lag(module_no) over (partition by product_id order by module_no) prev_module_no
      , lag(product_status) over (partition by product_id order by module_no) prev_product_status
  from product
)
where (case when current_module_no != prev_module_no
            then prev_module_no
       end) is not null
     and (case when current_product_status != prev_product_status
            then prev_product_status
          end) is not null
order by current_module_no;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM