考虑下表:
tbl_start_times
id mach_id start_time
-- ------- ----------
1 1 00:12
2 3 05:18
3 1 03:56
4 2 01:51
5 1 12:48
6 2 00:10
7 3 09:15
我想为每个mach_id返回id,mach_id和MIN(start_time)。
代码:
SELECT mach_id, MIN(start_time) FROM tbl_start_times GROUP BY mach_id
返回此结果:
mach_id start_time
------- ----------
1 00:12
3 05:18
2 00:10
如何将ID添加到我的结果中以便我得到它?
id mach_id start_time
-- ------- ----------
1 1 00:12
2 3 05:18
6 2 00:10
答案 0 :(得分:2)
在Postgres中有两种方法可以做到这一点:
使用Postgres特定的distinct on ()
运算符:
SELECT distinct on (match_id) id, match_id, start_time
FROM tbl_start_times
ORDER BY match_id, start_time;
或使用窗口功能:
with numbered_times as (
select id, match_id, start_time,
row_number() over (partition by match_id order by start_time) as rn
from tbl_start_times
)
select id, match_id, start_time
from numbered_times
where rn = 1;
当您使用distinct on(或最小/最大解决方案)时,这也可让您轻松选择“第二”或“第四”行,而不仅仅是“第一”或“最后”行
如果多行是“最低”(即同一match_id具有相同的最低时间)和您想要查看所有这些行,请使用dense_rank()
而不是{{1 }}
使用row_number()
的解决方案通常比使用窗口函数的相应解决方案更快。窗口函数是标准的SQL,然后在(几乎)所有现代DBMS上运行。两个版本通常比使用子查询或派生表的解决方案更快,因为只需要一次传递即可读取数据。
SQLFiddle示例:http://sqlfiddle.com/#!12/caa95/5
答案 1 :(得分:1)
您可以使用相关子查询执行此操作,如下所示:
SELECT id, mach_id, start_time
FROM tbl_start_times tst
WHERE start_time = (SELECT MIN(start time)
FROM tbl_start_times tst2
WHERE tst2.mach_id = tst.mach_id)
ORDER BY id
答案 2 :(得分:0)
试试这个:
SELECT t.id , t.mach_id, t.start_time
FROM tbl_start_times t,
(SELECT mach_id, MIN(start_time) as start_time
FROM tbl_start_times
GROUP BY mach_id) t1
WHERE t.mach_id=t1.mach_id AND t.start_time=t1.start_time