[英]Is there any way to print previous value of column and changed values in one row in oracle SQL
您拥有基本查询,并且很少使用主要用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.