Sql 按查询返回分组中的多个列
考虑下表:Sql 按查询返回分组中的多个列,sql,postgresql,Sql,Postgresql,考虑下表: 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 我想为每个马赫数返回id、马赫数id和MIN(
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
我想为每个马赫数返回id、马赫数id和MIN(开始时间)
守则:
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
试试这个:
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
试试这个:
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
试试这个:
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
试试这个:
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
您可以使用相关子查询执行此操作,如下所示:
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
您可以使用相关子查询执行此操作,如下所示:
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
您可以使用相关子查询执行此操作,如下所示:
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
您可以使用相关子查询执行此操作,如下所示:
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
在Postgres中有两种方法可以做到这一点: 使用Postgres-specific
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(或最小/最大解决方案)时轻松选择“第二行”或“第四行”,而不仅仅是“第一行”或“最后一行”
如果多行是“最低的”(即对于相同的匹配id具有相同的最低时间),并且您希望查看所有行,请使用densite\u rank()
而不是row\u number()
启用distinct的解决方案通常比使用窗口函数的相应解决方案速度更快。然而,window函数是标准的SQL,并且在(几乎)所有现代DBMS上运行。这两个版本通常都比使用子查询或派生表的解决方案快,因为读取数据只需要一次过程
SQLFIDLE示例:在Postgres中有两种方法可以做到这一点:
使用Postgres-specificdistinct-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(或最小/最大解决方案)时轻松选择“第二行”或“第四行”,而不仅仅是“第一行”或“最后一行”
如果多行是“最低的”(即对于相同的匹配id具有相同的最低时间),并且您希望查看所有行,请使用densite\u rank()
而不是row\u number()
启用distinct的解决方案通常比使用窗口函数的相应解决方案速度更快。然而,window函数是标准的SQL,并且在(几乎)所有现代DBMS上运行。这两个版本通常都比使用子查询或派生表的解决方案快,因为读取数据只需要一次过程
SQLFIDLE示例:在Postgres中有两种方法可以做到这一点:
使用Postgres-specificdistinct-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(或最小/最大解决方案)时轻松选择“第二行”或“第四行”,而不仅仅是“第一行”或“最后一行”
如果多行是“最低的”(即对于相同的匹配id具有相同的最低时间),并且您希望查看所有行,请使用densite\u rank()
而不是row\u number()
启用distinct的解决方案通常比使用窗口函数的相应解决方案速度更快。然而,window函数是标准的SQL,并且在(几乎)所有现代DBMS上运行。这两个版本通常都比使用子查询或派生表的解决方案快,因为读取数据只需要一次过程
SQLFIDLE示例:在Postgres中有两种方法可以做到这一点:
使用Postgres-specificdistinct-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(或最小/最大解决方案)时轻松选择“第二行”或“第四行”,而不仅仅是“第一行”或“最后一行”
如果多行是“最低的”(即对于相同的匹配id具有相同的最低时间),并且您希望查看所有行,请使用densite\u rank()
而不是row\u number()
启用distinct的解决方案通常比使用窗口函数的相应解决方案速度更快。然而,window函数是标准的SQL,并且在(几乎)所有现代DBMS上运行。这两个版本通常都比使用子查询或派生表的解决方案快,因为读取数据只需要一次过程
SQLFiddle示例:仅按id和马赫id分组?当按马赫id
分组时,id
可能不是唯一的。你能看到可能很多的id
s中的任何一个吗?我想要与MIN(开始时间)返回的值在同一行的id,这正是我想要的,Clodoaldo。只需按id和mach\u id分组?当你按mach\u id
分组时,id
可能不是唯一的。你能看到可能很多的id
s中的任何一个吗?我想要与MIN(开始时间)返回的值在同一行的id,这正是我想要的,Clodoaldo。只需按id和mach\u id分组?当你按mach\u id
分组时,id
可能不是唯一的。你能看到可能很多的id
s中的任何一个吗?我想要与MIN(开始时间)返回的值在同一行的id,这正是我想要的,Clodoaldo。只需按id和mach\u id分组?当你按mach\u id
分组时,id
可能不是唯一的。您是否同意看到可能存在的多个id
s中的任何一个?我希望id与MIN(开始时间)返回的值位于同一行,这正是我想要的,Clodoaldo。