sql - PostgreSQL 简单查询
问题描述
我的问题是编写一个查询(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
感谢帮助
解决方案
您可以使用聚合和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'
);
推荐阅读
- angular - 取消 mat-slide-toggle 两次更改切换状态
- python - 使用 tkinter 搜索 csv 文件
- apache-kafka - kafka如何处理不同批次的订购?
- amazon-web-services - 尝试使用 firefox 在 ec2 上打开网站时遇到云端 404 问题
- javascript - 如何一次控制所有ID?
- java - 如何确保使用 Spring JPA 以正确的顺序插入 SQL 数据?
- javascript - 如何使用 BootstrapVue 在 Vue 中的资源时间轴中将弹出框添加到完整日历事件?
- python - Python:字符串格式化和枚举?(以 Python 方式生成一个很好的多项式字符串表示)
- python - 有人可以解释一下为什么这个语句给出的输出为 'A', print (('A','B')[a == False]) 。a 的初始值 = True
- html - 如何创建具有线性渐变的动画圆形进度条?