SQL ORACLE获取冲销日期事务处理[已关闭]

guz6ccqo  于 2023-05-16  发布在  Oracle
关注(0)|答案(1)|浏览(128)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

4天前关闭。
Improve this question
列:item_number、category_id、transaction_number、transaction_effective_date
我试图从我的表“交易”中获取所有回溯交易的记录。
因此,当transaction_number列按item_number和category_id分区的升序排序时,如果transaction_effective date是回溯的(desc),则SQL应该获取所有4列

| ITEM_NUM | CATEGORY_NUM   |TRANSACTION_NUMBER|TRANSACTION_EFF_DATE
| -------- | -------------- |------------------|-------------------
| 100      | 55             |   1              |01-Jan-2022
| 100      | 55             |   2              |02-Jan-2022
| 100      | 55             |   3              |25-Feb-2022
| 100      | 55             |   4              |18-Feb-2022
| 100      | 55             |   5              |05-Mar-2022
| 200      | 12             |   1              |18-Feb-2022
| 200      | 12             |   2              |20-Feb-2022

item_number 100的transaction_number 4是回溯的,因此应返回item number 100及其category_number 55。
预期结果:

| ITEM_NUM | CATEGORY_NUM
| -------- | ------------
| 100      | 55

我是相当新的使用滞后功能,所以不能得到它的权利

6yt4nkrj

6yt4nkrj1#

对于同一项目,返回存在事务处理编号较低但日期较高的行的所有行。

select *
from mytable t
where exists
(
  select null
  from mytable t2
  where t2.item_num = t.item_num
  and t2.transaction_number < t.transaction_number
  and t2.transaction_eff_date > t.transaction_eff_date
)
order by item_num, transaction_number;

如果只需要项目编号和类别编号,则:

select distinct item_num, category_num
from mytable t
where exists
(
  select null
  from mytable t2
  where t2.item_num = t.item_num
  and t2.transaction_number < t.transaction_number
  and t2.transaction_eff_date > t.transaction_eff_date
)
order by item_num, category_num;

LAG相同的查询。它只检测两个连续行之间的不匹配,但这足以满足给定的任务。

select distinct item_num, category_num
from 
(
  select
    t.*, 
    lag(transaction_eff_date) over (partition by item_num order by transaction_eff_date) as prev_date
  from mytable t2
) with_prev_date
where date < prev_date
order by item_num, category_num;

相关问题