[英]How to select only one row per column value in SQL?
因此,我具有以下架構build_tasks:
id|building|queue_time|start_time|completion_time|status|createdAt|updatedAt|baseId|
我正在嘗試僅獲取狀態為“待決”的構建任務,其中狀態為“進行中”的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 | |
如何將結果減少為每個base_id僅根據最低queue_time選擇的行?
我不清楚您要輸出什么。 但是,如果要確定滿足條件的基本標識,則可以使用聚合:
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 ;
我不確定輸出中還想要什么。 使用聚合可以得到想要的東西。 另外, join
, in
或exists
可以得到您想要的。
但是,您不需要查看即可完成自己的工作。
您可以在輸出上方應用DISTINCT ON (baseId)
。
SELECT * FROM
(
SELECT DISTINCT ON (baseId) youroutput.*
FROM youroutput ORDER BY baseId,updatedAt
) as a;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.