使用 SQL 条件逻辑过滤值
Filter value with conditional logic with SQL
我有一个 table 如下:
store city status
a new york t
b paris f
b london t
c lisbon f
我想 select 这家商店,因为它只有 'f' 价值。如果商店同时有 'f' 和 't',则不算在内。期望的输出:
store status
c f
这是我尝试过的方法,但我仍然没有得到想要的输出:
SELECT store
FROM (
SELECT store, city,
CASE
WHEN status = 'f' THEN 1
WHEN status = 't' THEN -1
END AS status
FROM table) AS t
GROUP BY store
HAVING SUM(status) > 1
如有任何建议,我们将不胜感激!
您可以使用聚合和 having
:
select store
from t
group by store
having min(status) = 'f' and max(status) = 'f';
我有一个 table 如下:
store city status
a new york t
b paris f
b london t
c lisbon f
我想 select 这家商店,因为它只有 'f' 价值。如果商店同时有 'f' 和 't',则不算在内。期望的输出:
store status
c f
这是我尝试过的方法,但我仍然没有得到想要的输出:
SELECT store
FROM (
SELECT store, city,
CASE
WHEN status = 'f' THEN 1
WHEN status = 't' THEN -1
END AS status
FROM table) AS t
GROUP BY store
HAVING SUM(status) > 1
如有任何建议,我们将不胜感激!
您可以使用聚合和 having
:
select store
from t
group by store
having min(status) = 'f' and max(status) = 'f';