我使用的是Postgres 9.4,並希望做這樣的事情:SQL大於IN
movement_id|counter|standardized_output
---------------------------------------
1 | 3| 10
1 | 3| 12
1 | 5| 10
2 | 4| 5
我有以下查詢:
SELECT movement_id, counter, MAX(standardized_output) AS standardized_output
FROM "outputs"
WHERE "outputs"."user_id" = 1 AND "outputs"."movement_id" IN (1,2) AND (counter in (1,3,5))
GROUP BY movement_id, counter
這給了我:
movement_id|counter|standardized_output
---------------------------------------
1 | 3| 12
1 | 5| 10
但是我想找到的是MAX(standard_output)是counter >= (1,3,5)
。所以下面的結果:
movement_id|counter|standardized_output
---------------------------------------
1 | 1| 12 (MAX value where movement_id is 1 and counter is >=1)
1 | 3| 12 (MAX value where movement_id is 1 and counter is >=3)
1 | 5| 10 (MAX value where movement_id is 1 and counter is >=5)
2 | 1| 5 (MAX value where movement_id is 2 and counter is >=1)
2 | 3| 5 (MAX value where movement_id is 2 and counter is >=3)
2 | 5| null (MAX value where movement_id is 2 and counter is >=5)
(小編輯:movement_id是IN,不等於)
您是否想要standard_output超過提供的數字中的每個值或至少多個值? – ogres
什麼是你的表中的數據? – tharif
因此,即使沒有'movement_id'和'counter'的相應組合,您每個movement_id都需要三行(因爲您的IN列表包含三個計數器值)(您的樣本數據不會顯示例如2,1的值) –