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

时间:2018-02-04 14:29:42

标签: sql postgresql

所以,我有以下架构build_tasks:

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

我正在尝试只获取状态为“pending”的构建任务,其中没有build_task在状态为“正在进行中”具有相同的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是视图:

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      |       |

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

2 个答案:

答案 0 :(得分:0)

我不清楚你想要什么输出。但是,如果要确定符合条件的基本ID,可以使用聚合:

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可以获得您想要的效果。

但是,您不需要一个视图来完成您正在做的事情。

答案 1 :(得分:0)

您可以在输出上方应用DISTINCT ON (baseId)

SELECT * FROM
(

SELECT DISTINCT ON (baseId)  youroutput.*
  FROM youroutput ORDER BY baseId,updatedAt
) as a;