如何在不同的表上使用 group_concat 和 group by 求和
How to SUM using group_concat with group by on differents tables
我有 3 个表:用户、工具,tool_assigned:
用户:
id
name
department
1
john
a
2
peter
b
3
paul
a
工具:
id
name
1
tool1
2
tool2
3
tool3
Tool_assigned:
id
id_user
id_tool
quantity
1
1
1
1
2
1
2
3
3
1
3
1
4
2
1
2
5
2
2
3
6
2
3
1
7
3
1
2
8
3
2
1
9
3
3
1
我需要得到这个结果:
id_tool
tool_name
total_quantity
summary_department
1
tool1
5
a-1, b-2, c-2
2
tool2
7
a-3, b-3, c-1
3
tool3
3
a-1, b-1, c-1
我已经尝试执行可以获得该结果的查询,但到目前为止我得到的最好结果是:
SELECT tool.id, tool.name, sum(tool_assigned.quantity) as total_quantity,
group_concat(user.deparment, '-', tool_asigned.quantity) as summary_department
FROM tool_assigned
INNER JOIN users ON tool_assigned.id_user = user.id
INNER JOIN tools ON tool_assigned.id_tool = tool.id
GROUP BY tools.name, users.department
请帮帮我,我已经试过了。
您需要两个级别的聚合:首先按工具和部门,然后仅按工具。您可以这样表述:
select t.id, t.name as tool_name,
sum(x.quantity) as total_quantity,
group_concat(x.info order by x.department) as summary_department
from tools t
inner join (
select ta.id_tool, u.department, concat(u.name, '-', sum(t.quantity)) as info
from tool_assigned ta
inner join users on ta.id_user = u.id
group by ta.id_tool, u.department
) x on x.id_tool = t.id
group by t.id
您还可以使用 with
语句
with tools_per_dept as (
SELECT users.department as dept,
tool.id as tool_id,
tool.name as tool_name,
sum(tool_assigned.quantity) as total_quantity
FROM tool_assigned
INNER JOIN users ON tool_assigned.id_user = user.id
INNER JOIN tools ON tool_assigned.id_tool = tool.id
GROUP BY tools.name, users.department
)
select tool_id,
tool_name,
sum(total_quantity) as total,
group_concat(dept, '-', total_quantity) as summary_department
from tools_per_dept
group by tool_id, tool_name
我有 3 个表:用户、工具,tool_assigned:
用户:
id | name | department |
---|---|---|
1 | john | a |
2 | peter | b |
3 | paul | a |
工具:
id | name |
---|---|
1 | tool1 |
2 | tool2 |
3 | tool3 |
Tool_assigned:
id | id_user | id_tool | quantity |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1 | 2 | 3 |
3 | 1 | 3 | 1 |
4 | 2 | 1 | 2 |
5 | 2 | 2 | 3 |
6 | 2 | 3 | 1 |
7 | 3 | 1 | 2 |
8 | 3 | 2 | 1 |
9 | 3 | 3 | 1 |
我需要得到这个结果:
id_tool | tool_name | total_quantity | summary_department |
---|---|---|---|
1 | tool1 | 5 | a-1, b-2, c-2 |
2 | tool2 | 7 | a-3, b-3, c-1 |
3 | tool3 | 3 | a-1, b-1, c-1 |
我已经尝试执行可以获得该结果的查询,但到目前为止我得到的最好结果是:
SELECT tool.id, tool.name, sum(tool_assigned.quantity) as total_quantity,
group_concat(user.deparment, '-', tool_asigned.quantity) as summary_department
FROM tool_assigned
INNER JOIN users ON tool_assigned.id_user = user.id
INNER JOIN tools ON tool_assigned.id_tool = tool.id
GROUP BY tools.name, users.department
请帮帮我,我已经试过了。
您需要两个级别的聚合:首先按工具和部门,然后仅按工具。您可以这样表述:
select t.id, t.name as tool_name,
sum(x.quantity) as total_quantity,
group_concat(x.info order by x.department) as summary_department
from tools t
inner join (
select ta.id_tool, u.department, concat(u.name, '-', sum(t.quantity)) as info
from tool_assigned ta
inner join users on ta.id_user = u.id
group by ta.id_tool, u.department
) x on x.id_tool = t.id
group by t.id
您还可以使用 with
语句
with tools_per_dept as (
SELECT users.department as dept,
tool.id as tool_id,
tool.name as tool_name,
sum(tool_assigned.quantity) as total_quantity
FROM tool_assigned
INNER JOIN users ON tool_assigned.id_user = user.id
INNER JOIN tools ON tool_assigned.id_tool = tool.id
GROUP BY tools.name, users.department
)
select tool_id,
tool_name,
sum(total_quantity) as total,
group_concat(dept, '-', total_quantity) as summary_department
from tools_per_dept
group by tool_id, tool_name