我有一张桌子:
store || item || value
----------------------
x || a || 100
----------------------
x || b || 90
----------------------
x || c || 100
----------------------
y || a || 80
----------------------
y || b || 90
----------------------
y || e || 100
----------------------
z || a || 100
----------------------
z || b || 50
----------------------
我需要选择所有至少有两个项目的商店,彼此相差10个单位。
因此对于示例表:
x具有项目a和b,以及b和c,以及a和c在10个单位内
y项目a和b在10个单位内,b和e在10个单位内
z没有10个单位内的物品。
结果将是: x和y
答案 0 :(得分:0)
SELECT t1.store, t1.item, t1.value
FROM tbl t1
WHERE EXISTS (
SELECT 1
FROM tbl t2
WHERE t2.store = t1.store
AND t2.item <> t1.item
AND t2.value BETWEEN t1.value - 10 AND t1.value + 10
)
和/或
SELECT DISTINCT t1.store, t1.item, t1.value
FROM @t t1
JOIN @t t2 ON t2.store = t1.store
AND t2.item <> t1.item
AND t2.value - 10 <= t1.value
AND t2.value + 10 >= t1.value