我需要确定以下每个项目与各个修订版的区别,如何比较同一列中的数据-基于旧修订版的值?
T1:
ID Value Rev
1 1001 01
1 1002 02
1 1001 03
1 1002 03
2 1003 02
2 1004 03
3 1003 01
3 1005 02
3 1005 03
4 1002 01
对于#1:版本1-1001,版本2-1002,版本3-1001&1002
对于#2:Rev 2-1003,Rev 3-1004
对于#3:版本1-1003,版本2:1005
所需的输出:
ID Value Rev
1 1001 01
1 1002 02
1 1001 03
1 1002 03
2 1003 02
2 1004 03
3 1003 01
3 1005 02
答案 0 :(得分:3)
您可以尝试使用lag()和lead()
select id, value, rev from
(
select * , lead(value) over(partition by id order by rev) as val1,
lag(value) over(partition by id order by rev) as val2
from t1
)A
where (value<>val2 or value<>val1)
输出:
id value rev
1 1001 01
1 1002 02
1 1001 03
1 1002 03
2 1003 02
2 1004 03
3 1003 01
3 1005 02
答案 1 :(得分:0)
您只需执行self-join
并使用Distinct
即可达到以下结果:
SELECT DISTINCT T11.ID, T11.VALUE, T11.rev FROM
T1 T11
JOIN T1 T12
ON (T11.ID = T12.ID AND T11.VALUE <> T12.VALUE)
ORDER BY T11.ID, T11.rev
干杯!
答案 2 :(得分:0)
我认为您希望ID包含多行。如果是这样:
select t.*
from (select t.*, count(*) over (partition by id) as cnt
from t
) t
where cnt > 1
order by id, rev;
或使用exists
:
select t.*
from t
where exists (select 1
from t tt
where tt.id = t.id and
(tt.value <> t.value or
tt.rev <> t.rev
)
)
order by id, rev;