[英]How does one compare a row with its preceding row in Oracle SQL?
我正在尝试查找表中每一行与其前一行之间的差异。
当我说差异时,我的意思是两个值是否相同。
我不确定是否需要for循环,并且该表是否具有30到40列,这是否意味着我必须为这30-40列中的每一个编写检查?
第4行-ID1-数据1-数据2-数据3 ..
for i = 2 .. 3 is Row[i][data1] != Row[i-1][data1], Row[i][data2] != Row[i-1][data2]....
需要对具有相同非唯一ID的所有记录进行此检查。 即。 ID1可以存在多个记录。 需要将ID1的行与ID1的其他行进行比较。
您可以使用Oracle lag
分析功能。 让我们假设您的表是这样的:
CREATE TABLE tbl (no int, id int, val1 int, val2 int)
其中, no
是订购数据的字段, id
是您的非唯一标识符(组标识符), val1
和val2
是值。 比您可以使用此查询来查找每行是否与其组中的前一行重复:
select no, id, val1, val2,
case when
lag(val1,1) over (partition by id order by no) = val1 and
lag(val2,1) over (partition by id order by no) = val2
then 1 else 0 end duplicate
from tbl
order by no
尝试这个 !!
将表格与其自己的PK进行比较
select t1.value - t2.value from table t1, table t2
where t1.primaryKey = t2.primaryKey - 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.