[英]SQL Select based on difference between two rows
我試圖找出如何輕松地基於兩行之間的差異選擇記錄。
我有一張這樣的桌子。
Time | Store | Code | % | Ranked |
------------------------------------------
13:50 | Swindon | 33 | 32.578 | 1 |
13:50 | Reading | 31 | 29.438 | 2 |
13:50 | Bath | 32 | 28.221 | 3 |
14:50 | Swindon | 33 | 32.100 | 1 |
14:50 | Reading | 32 | 30.987 | 2 |
14:50 | Bath | 32 | 28.335 | 3 |
我需要根據不同時間之間商店之間的百分比差異來做不同的報告。
一個例子。
從排名2的商店中選擇所有排名1的商店,其中>=
到3
因此,在這種情況下,它只會選擇13:50 Swindon,因為%的差異剛剛超過3%。
還有其他變體,但是我敢肯定,一旦得到答案,我自己就能解決剩下的一切。
我知道普通的select語句,但我認為我必須進行聯接,但不確定如何執行。
根據上面給出的信息,我想是這樣的:
select *
from table t1
inner join table t2 on t1.Time = t2.Time and t1.Ranked = 1 and t2.Ranked = 2
where (t1.% - t2.%) > 3
列名有些偏離,但我想排名是基於時間的。
SELECT R1.store,
R1.timestamp
FROM store_tbl R1,
store_tbl R23
WHERE R1.ranked = 1
AND R23.ranked = 2
AND R1.timestamp = R23.timestamp
AND ( R1.percentage - R23.percentage ) > 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.