sql - 如何在 SQL 中每列值只选择一行?

标签 sql postgresql

因此,我有以下架构 build_tasks:

id|building|queue_time|start_time|completion_time|status|createdAt|updatedAt|baseId|

我试图只获取状态为“待定”的构建任务,其中没有处于“进行中”状态的具有相同 baseId 的构建任务。

到目前为止,我设法获得了一个表,其中包含所有待处理的构建任务,其中没有正在进行的构建任务。这是查询:

select * from (select build_tasks.* from build_tasks
            where status = 'pending') as p
left join in_progress_build_tasks ipbt on p."baseId" = ipbt."baseId"
      where ipbt."baseId" is null;

其中 in_progress_build_tasks 是 View :

CREATE OR REPLACE VIEW "public".in_progress_build_tasks AS
 SELECT DISTINCT build_tasks."baseId"
   FROM build_tasks
  WHERE build_tasks.status = 'in-progress'::enum_build_tasks_status;

哪个用于表:

id |building            |queue_time          |start_time          |completion_time     |status      |createdAt           |updatedAt           |baseId |
---|--------------------|--------------------|--------------------|--------------------|------------|--------------------|--------------------|-------|
7  |resource01_refinery |2018-02-04 14:09:49 |                    |                    |pending     |2018-02-04 14:09:49 |2018-02-04 14:09:49 |1      |
10 |resource01_refinery |2018-02-04 14:45:07 |                    |                    |pending     |2018-02-04 14:45:07 |2018-02-04 14:45:07 |1      |
6  |resource01_refinery |2018-02-04 14:07:32 |2018-02-04 14:07:58 |2018-02-04 14:08:08 |in-progress |2018-02-04 14:07:32 |2018-02-04 14:08:09 |1      |
12 |resource01_refinery |2018-02-04 14:46:04 |2018-02-04 14:46:04 |2018-02-04 14:46:04 |successful  |2018-02-04 14:46:04 |2018-02-04 14:58:28 |2      |
8  |resource01_refinery |2018-02-04 14:10:29 |2018-02-04 14:10:29 |2018-02-04 14:10:39 |successful  |2018-02-04 14:10:29 |2018-02-04 14:10:39 |2      |
9  |resource01_refinery |2018-02-04 14:11:38 |                    |                    |pending     |2018-02-04 14:11:38 |2018-02-04 14:11:38 |2      |
11 |resource01_refinery |2018-02-04 14:45:14 |                    |                    |pending     |2018-02-04 14:45:14 |2018-02-04 14:45:14 |2      |
13 |resource01_refinery |2018-02-04 15:11:16 |                    |                    |pending     |2018-02-04 15:11:16 |2018-02-04 15:11:16 |3      |
15 |resource01_refinery |2018-02-04 15:11:19 |                    |                    |pending     |2018-02-04 15:11:19 |2018-02-04 15:11:19 |3      |
14 |resource01_refinery |2018-02-04 15:11:18 |                    |                    |pending     |2018-02-04 15:11:18 |2018-02-04 15:11:18 |3      |

给我输出:

id |building            |queue_time          |start_time |completion_time |status  |createdAt           |updatedAt           |baseId |baseId |
---|--------------------|--------------------|-----------|----------------|--------|--------------------|--------------------|-------|-------|
9  |resource01_refinery |2018-02-04 14:11:38 |           |                |pending |2018-02-04 14:11:38 |2018-02-04 14:11:38 |2      |       |
11 |resource01_refinery |2018-02-04 14:45:14 |           |                |pending |2018-02-04 14:45:14 |2018-02-04 14:45:14 |2      |       |
13 |resource01_refinery |2018-02-04 15:11:16 |           |                |pending |2018-02-04 15:11:16 |2018-02-04 15:11:16 |3      |       |
14 |resource01_refinery |2018-02-04 15:11:18 |           |                |pending |2018-02-04 15:11:18 |2018-02-04 15:11:18 |3      |       |
15 |resource01_refinery |2018-02-04 15:11:19 |           |                |pending |2018-02-04 15:11:19 |2018-02-04 15:11:19 |3      |       |

如何将结果减少到每个 base_id 只有 1 行,根据最低的 queue_time 选择?

最佳答案

我不清楚你想要输出什么。但是如果你想找出满足你条件的base ids,你可以使用聚合:

select bt.baseid
from build_tasks bt
group by bt.baseid
having sum( (bt.status = 'pending'::enum_build_tasks_status)::int) > 0 and
       sum( (bt.status = 'in-progress'::enum_build_tasks_status)::int) = 0 ;

我不确定你还想要什么输出。您可能可以使用聚合获得想要的东西。或者,joininexists 可以获得您想要的。

但是,您不需要 View 来完成您正在做的事情。

关于sql - 如何在 SQL 中每列值只选择一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48609130/

相关文章:

sql - 多语言列上的全文搜索索引

mysql - 如何通过聚合和标志来编写?

sql - 安装在 Windows 2008 64 位机器上的 SQL Server 2008 R2 上的 Openrowset

java - 优化数据库使用

windows - 有什么方法可以修复 pgpass.conf 在 Windows 上正常工作吗?

mysql - 一张表与三张表中的许多空值

sql - T-SQL - 必须在分组依据或聚合函数中有术语才能选择

postgresql - PostgreSQL 8.3 上的 Bigint

postgresql - 无法\复制到具有默认 clock_timestamp 的 postgresql 表中

python - Django 不迁移应用程序