同一张表中的行之间的差异(Oracle SQL)

Posted

技术标签:

【中文标题】同一张表中的行之间的差异(Oracle SQL)【英文标题】:Difference between the rows in the same table (Oracle SQL) 【发布时间】:2019-07-12 08:33:41 【问题描述】:

我真的很纠结在 Oracle SQL 中比较同一个表中的行。我需要获取之前/之后样式的所有更改。

我有这样的表:

id      date        name    action
1       01-01-2011  Alex    smth
1       05-01-2011  Alexx   smth
1       07-01-2011  Alexa   smth2
2       02-01-2012  Leo     smth3
2       05-01-2012  Leon    smth3

我需要得到这个:

id      date        field   before  after
1       05-01-2011  name    Alex    Alexx
1       07-01-2011  name    Alexx   Alexa
1       07-01-2011  action  smth    smth2
2       05-01-2012  name    Leo     Leon

我尝试将表与自身进行内部连接。我找到的方法(请在此处找到)应该可以帮助我将行与下一个内部连接返回“无效数字”错误。

Mb 有更简单的方法来完成这项任务吗? 你能帮帮我吗?

select t1.id from tablename t1 
inner join tablename t3 on t1.id = t3.id + 1

【问题讨论】:

您能解释一下为什么结果中只有这一行:field=actionaction smth smth2,但:smthsmth3 缺失?我无法理解逻辑。 因为我只需要选择已更改的字段。 【参考方案1】:

你可以使用lag()解析函数:

with t( id, "date", name, action ) as
(
 select 1, date'2011-01-01','Alex','smth' from dual union all
 select 1, date'2011-01-05','Alexx','smth' from dual union all
 select 1, date'2011-01-07','Alexa','smth2' from dual union all
 select 2, date'2012-01-02','Leo','smth3' from dual union all
 select 2, date'2012-01-05','Leon','smth3' from dual 
), t2 as
(
select t.*, 
       lag(name,1,null) over (partition by id order by id, "date") as lg_name,
       lag(action,1,null) over (partition by id order by id, "date") as lg_action
  from t
), t3 as
(
select id, "date", 'name' as field, lg_name as before, name as after
  from t2 where name != lg_name
union all
select id, "date", 'action', lg_action, action 
  from t2 where action != lg_action
)
select * from t3 order by id, "date";

ID  date        FIELD   BEFORE  AFTER
--  ---------   -----   ------  ------
1   05-JAN-11   name    Alex    Alexx
1   07-JAN-11   action  smth    smth2
1   07-JAN-11   name    Alexx   Alexa
2   05-JAN-12   name    Leo     Leon

Demo

【讨论】:

非常感谢!这是我最需要的东西,因为我的表中有更多字段。【参考方案2】:

我首先根据日期(每个 id)为每一行分配一个序数,然后将每一行与其下一行自我连接起来。一旦你有了它,你可以使用几个case 语句来生成前后数据:

WITH cte AS (
    SELECT id, date, name, action, 
           ROW_NUMBER() OVER (PARTITION BY id ORDER BY date ASC) AS rn
    FROM   mytable
)
SELECT a.id,
       a.date
       CASE WHEN b.name != a.name THEN 'name' ELSE 'action' END AS field,
       CASE WHEN b.name != a.name THEN b.name ELSE b.action END AS before,
       CASE WHEN b.name != a.name THEN a.name ELSE a.action END AS after
FROM   cte b
JOIN   cte a ON b.id = a.id AND b.rn = a.rn + 1

【讨论】:

如果同一条记录的两个字段都在更改,则此方法不起作用(对于给定的数据,此查询仅返回 3 条记录)。还缺少所有 case 语句的 END 关键字。【参考方案3】:

LAG() 显然是正确的使用方法。但是,我会先取消透视:

select id, date, field, prev_value as before, value as after
from (select id, date, field, value,
             lag(value) over (partition by id, field order by date) as prev_value
      from ((select id, "date", 'name' as field, name as value
             from t
            ) union all
            (select id, "date", 'action' as field, action as value
             from t
            ) 
           ) t
      ) t
where prev_value <> value;

在较新的 Oracle 版本中,这可以使用横向连接来简化:

select id, date, field, prev_value as before, value as after
from (select t.id, t.date, x.field, x.value,
             lag(x.value) over (partition by t.id, x.field order by date) as prev_value
      from t cross join lateral
           (select 'name' as field, name as value from dual union all
            select 'action', action from dual
           ) x
      ) t
where prev_value <> value;

【讨论】:

非常感谢!它也非常有用。抱歉,无法匹配第二个答案 :(【参考方案4】:

由于您有两个要比较的字段,我使用 UNION ALL 完成了如下:

WITH CTE AS (
    SELECT
        ID,
        DATE1,
        NAME1,
        ACTION,
        ROW_NUMBER() OVER(
            PARTITION BY ID
            ORDER BY
                DATE1 ASC
        ) AS RN
    FROM
        MYTABLE
)
-- 
 SELECT
      A.ID,
      B.DATE1,
      'name' AS FIELD,
      A.NAME1   AS BEFORE,
      B.NAME1   AS AFTER
  FROM
      CTE A
      JOIN CTE B ON B.ID = A.ID
                    AND B.RN = A.RN + 1
                        AND B.NAME1 != A.NAME1
  UNION ALL
  SELECT
      A.ID,
      B.DATE1,
      'action' AS FIELD,
      A.ACTION   AS BEFORE,
      B.ACTION   AS AFTER
  FROM
      CTE A
      JOIN CTE B ON B.ID = A.ID
                    AND B.RN = A.RN + 1
                        AND B.ACTION != A.ACTION
  ORDER BY
      ID,
      DATE1

输出:

db<>fiddle demo

干杯!!

【讨论】:

以上是关于同一张表中的行之间的差异(Oracle SQL)的主要内容,如果未能解决你的问题,请参考以下文章

从同一张表中找出两组数据之间的差异

计算同一张表中SQL中两个时间戳的差值

Oracle - 两个表中两行之间的差异或变化

根据同一张表中的其他数据验证插入的数据(Oracle)

如何比较同一张表(SQL Server)中的 2 行?

如何使用触发器在同一张表中插入新行(Oracle PL/SQL 10G)?