postgresql - 如何找出每个 integration_id 组的 name 列被批准的最低日期?
问题描述
id | integration_id | date | name
(1, 1, 2021-04-14 18:04:18.167+05:30, approved)
(2, 1, 2021-04-15 18:04:18.167+05:30, approved)
(3, 2, 2021-04-16 18:04:18.167+05:30, not approved)
(4, 2, 2021-04-17 18:04:18.167+05:30, approved)
如果有多个这样的 integration_id,我们只需要选择已批准的行,我如何编写查询以从 postgresql 中的每个集成 id 组中获取最小日期行。
id | integration_id | date | name
(1, 1, 2021-04-14 18:04:18.167+05:30, approved)
(4, 2, 2021-04-17 18:04:18.167+05:30, approved)
这应该是我的最终答案。
解决方案
在 Postgres 上,DISTINCT ON
在这里派上用场:
SELECT DISTINCT ON (integration_id) *
FROM yourTable
WHERE name = 'approved'
ORDER BY integration_id, date;
推荐阅读
- php - Laravel 使用复选框更新多条记录
- content-management-system - 如何在 contentstack cms 中创建页面 url
- c - 我是编程初学者,我写了一个代码,它给出了完美的输出,但在线判断没有接受。为什么会发生?(Codeforces问题69A。)
- c# - ObervableCollection、CollectionChanged 和 PropertyChanged
- database - SQL Server / Entity Framework Core:父级有一个子键作为搜索的外键会更好吗?
- reactjs - Material UI 中的 TextInput 组件在 Wordpress 网站上的外观与在我的本地版本上不同
- vue.js - Vue.js | 如何访问字段:脚本标签中的类型属性
- python - 如何从 Django 中永久删除记录?
- scrapy - 尝试在不启动scrapy项目但从 .py 文件的情况下下载文件。在 python 文件中创建了自定义管道,这个错误是提到的
- jenkins - Jenkins miniOrange SAML 2.0 登录失败