我有下表:
X Y Z T
__________________
A 2
B 5 Z is UNIQUE
C 8
A 3
B 6
A 4
C 1
我想根据Z值将表格减少到下一个;
X Y Z T
__________________
A 2
A 3
A 4
B 5
B 6
我怎么能想要构造表如下?
差异btw 3和2是1,而PK,即X,是相同的,所以取“A 2”和“A 3”
差异btw 4和3是1,而PK,即X,是相同的,所以取“A 4”和“A 3”
差异btw 6和5是1,而PK,即X,是相同的,所以取“B 6”和“B 5”
我该怎么做?
答案 0 :(得分:2)
SELECT a.*
FROM TableX AS a
WHERE EXISTS
( SELECT *
FROM TABleX AS b
WHERE b.X = a.X
AND ( b.Z = a.Z + 1
OR b.Z = a.Z - 1
)
)