gpt4 book ai didi

postgresql - 从具有相似数据的行中选择一行

转载 作者:行者123 更新时间:2023-11-29 14:34:03 26 4
gpt4 key购买 nike

我希望有人能够用knex.js解决我的问题

有一个表-“task_history”,它有以下列:id, task_id, duration, date, start_time, finish_time

假设我在 task_history 表中有三行:

row 1 - id(1) task_id(3333), duration(200), date(20171204)
,start_time("2017-10-19 21:00:00+11"), finish_time("2017-12-05 00:59:18.943+11")

row 2 - id(2) task_id(3333), duration(200), date(20171204)
,start_time("2017-10-19 21:00:00+11"), finish_time()

row 3 - id(3) task_id(4444), duration(100), date(20171204)
,start_time("2017-10-19 21:00:00+11"), finish_time()

如您所见,第 1 行和第 2 行几乎相同,除了 id、finish_time。第 2 行的 finish_time 列具有“空”值。

当我尝试以下查询时,它会返回第 2 行和第 3 行。

select * from task_history where date=20171204 and finish_time is null 

但是,这不是我想要的。如果它的 task_id 存在于另一行中,我不想获取任何行,但它对 finish_time 列也有值。

查询应该只返回第 3 行。但是第 1 行和第 2 行,因为第 1 行具有 finish_time 值和与第 2 行相同的 task_id

提前致谢!

最佳答案

试试 DISTINCT ON?..

select  distinct on (task_id) * from task_history 
where date=20171204
order by tastk_id, finish_time nulls last;

关于postgresql - 从具有相似数据的行中选择一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com