我的问题是编写一个查询(PostgreSQL)以获取一个“ propno”,其中“ newsname”不等于“ NYT”(答案是40)。
newsname | propno | dateadvert | cost
----------------------+--------+------------+------
NYT | 30 | 2018-01-01 | 10.2
NYT | 30 | 2018-01-10 | 15.2
NYT | 10 | 2018-01-02 | 20.2
NYT | 20 | 2018-02-01 | 10.2
Guardian | 40 | 2018-02-10 | 100
Guardian | 10 | 2018-01-02 | 13.2
Guardian | 30 | 2018-01-10 | 10.8
感谢帮助
答案 0 :(得分:1)
您可以使用聚合和having
:
select propno
from t
group by propno
having count(*) filter (where newsname = 'NYT') = 0;
如果您有一个单独的表,每个propno
有一行,那么我建议使用not exists
:
select p.propno
from props p
where not exists (select 1
from t
where t.propno = p.propno and
t.newname = 'NYT'
);
答案 1 :(得分:0)
您可以使用存在的
select propno
from props p
where not exists (
select 1
from props t
where t.propno = p.propno
and t.newname = 'NYT'
);
答案 2 :(得分:0)
HAVING
可用于过滤'NYT'
:
SELECT propno
FROM tab_name
GROUP BY propno
HAVING SUM(newsname='NYT')=0;