如何在 Sql 中分组 table?
How to group a table in Sql?
我有以下 table, Tasks
:
ID
Task
Type
Time_Taken
1
task1
type1
01h:00m
2
task1
type2
02h:00m
3
task2
type1
00h:30m
4
task2
type2
00h:30m
5
task3
type1
01h:00m
6
task3
type2
04h:00m
我想从 Tasks
table 创建一个新的 table,应该是
Task
type1_time
type2_time
task1
01h:00m
02h:00m
task2
00h:30m
00h:30m
task3
01h:00m
04h:00m
你能帮我建立一个 sql 查询吗?
我假设此 SQL 查询是在 MySQL 数据库中处理的。请参考以下查询:
select Task,
sum(if(Type='type1', Time_Taken,0)) as type1_time,
sum(if(Type='type2', Time_Taken,0)) as type2_time from Tasks
group by Task
-- Solution INNER JOIN
SELECT type1.Task, type1.Time_Taken as 'type1_time', type2.Time_Taken as 'type2_time'
FROM Task type1
INNER JOIN Task type2 on type1.Task = type2.Task
WHERE type1.Type = 'type1'
AND type2.Type = 'type2';
--Solution GROUP_CONCAT
SELECT
t.Task,
GROUP_CONCAT(IF(t.Type = "type1", t.Time_Taken, NULL)) AS 'type1_time',
GROUP_CONCAT(IF(t.Type = "type2", t.Time_Taken, NULL)) AS 'type2_time'
FROM Task t
GROUP BY t.Task;
演示:https://paiza.io/projects/e/V9GtkWoJQAqewynjRXZ9-Q?theme=twilight
我的查询会给您预期的结果。演示 link:db-fiddle
SELECT Task,
SEC_TO_TIME(sum(if(Type='type1', TIME_TO_SEC(Time_Taken),0))) as type1_time,
SEC_TO_TIME(sum(if(Type='type2', TIME_TO_SEC(Time_Taken),0))) as type2_time
FROM Tasks
GROUP BY Task
每个任务和类型都有一个条目。当你想按任务分组时,你可以使用 MIN
或 MAX
来访问类型的值,因为只有一个。
select
task,
min(case when type = 'type1' then time_taken end) as type1time,
min(case when type = 'type2' then time_taken end) as type2time
from tasks
group by task
order by task;
这称为条件聚合,因为我们在条件上使用聚合函数(此处:当行与所需类型匹配时)。
我有以下 table, Tasks
:
ID | Task | Type | Time_Taken |
---|---|---|---|
1 | task1 | type1 | 01h:00m |
2 | task1 | type2 | 02h:00m |
3 | task2 | type1 | 00h:30m |
4 | task2 | type2 | 00h:30m |
5 | task3 | type1 | 01h:00m |
6 | task3 | type2 | 04h:00m |
我想从 Tasks
table 创建一个新的 table,应该是
Task | type1_time | type2_time |
---|---|---|
task1 | 01h:00m | 02h:00m |
task2 | 00h:30m | 00h:30m |
task3 | 01h:00m | 04h:00m |
你能帮我建立一个 sql 查询吗?
我假设此 SQL 查询是在 MySQL 数据库中处理的。请参考以下查询:
select Task,
sum(if(Type='type1', Time_Taken,0)) as type1_time,
sum(if(Type='type2', Time_Taken,0)) as type2_time from Tasks
group by Task
-- Solution INNER JOIN
SELECT type1.Task, type1.Time_Taken as 'type1_time', type2.Time_Taken as 'type2_time'
FROM Task type1
INNER JOIN Task type2 on type1.Task = type2.Task
WHERE type1.Type = 'type1'
AND type2.Type = 'type2';
--Solution GROUP_CONCAT
SELECT
t.Task,
GROUP_CONCAT(IF(t.Type = "type1", t.Time_Taken, NULL)) AS 'type1_time',
GROUP_CONCAT(IF(t.Type = "type2", t.Time_Taken, NULL)) AS 'type2_time'
FROM Task t
GROUP BY t.Task;
演示:https://paiza.io/projects/e/V9GtkWoJQAqewynjRXZ9-Q?theme=twilight
我的查询会给您预期的结果。演示 link:db-fiddle
SELECT Task,
SEC_TO_TIME(sum(if(Type='type1', TIME_TO_SEC(Time_Taken),0))) as type1_time,
SEC_TO_TIME(sum(if(Type='type2', TIME_TO_SEC(Time_Taken),0))) as type2_time
FROM Tasks
GROUP BY Task
每个任务和类型都有一个条目。当你想按任务分组时,你可以使用 MIN
或 MAX
来访问类型的值,因为只有一个。
select
task,
min(case when type = 'type1' then time_taken end) as type1time,
min(case when type = 'type2' then time_taken end) as type2time
from tasks
group by task
order by task;
这称为条件聚合,因为我们在条件上使用聚合函数(此处:当行与所需类型匹配时)。