我的查询有问题希望你们能帮助我, 表:
ID | Value
1 | F
2 | V
2 | F
3 | V
4 | F
4 | V
我希望我的查询在ID同时包含F和V时返回P,
ID | Value
1 | F
2 | P
3 | V
4 | P
我试过CASE
WHEN VALUE ='F' AND VALUE ='V' THEN 'P'
ELSE VALUE END
但它没有奏效。
答案 0 :(得分:2)
您需要分组子句,然后处理案例
SELECT id,
CASE WHEN MIN(ds._value) = 'F' AND MAX(ds._value) = 'V' THEN 'P' ELSE MIN(ds._value) END AS _value
FROM (
--making the data set
SELECT '1' AS id,'F' AS _value UNION SELECT '2','V' UNION
SELECT '2','F' UNION SELECT '3','V' UNION SELECT '4','F' UNION SELECT '4','V'
) ds
GROUP BY id
答案 1 :(得分:1)
使用group by
和一些条件逻辑:
select id,
(case when min(value) = 'F' and max(value) = 'V' then 'P'
else min(value)
end) as value
from t
group by id;
这假设唯一的值为'F'
和'V'
,如您的示例所示。
答案 2 :(得分:-1)
你可以尝试一下吗?
for q in Color:
stmt = "SELECT Date, %s FROM mytable WHERE %s IS NOT NULL" %(q,q)
d.execute(stmt)