下午好/早上我有一个逻辑错误,即如何正确输出每台设备的 总使用量 以及 总回报数< / em> 该设备。
此表的示例:( 每个表的总数量 )
设备表:
SELECT e.equip_ID,e.equip_Name,u.unit_Name,e.equip_Quantity
FROM equipment e
INNER JOIN unit u ON u.unit_ID = e.unit_ID
设备使用表:
SELECT eu.usage_ID,e.equip_Name,eu.usage_Quantity
FROM equipment_usage eu
INNER JOIN equipment e ON e.equip_ID = eu.equip_ID
设备退货用量表
SELECT * FROM equipment_return
一旦项目使用设备,设备将记录在&#34; equipment_usage &#34;表然后如果项目将特定设备返回设备记录仪进入&#34; 设备_返回&#34;
这是我的疑问,我对如何输出数据感到困惑: Sample Data And Query
SELECT
e.equip_ID,
e.equip_Name,
u.unit_Name,
e.equip_Quantity total_Quantity,(
SELECT IF(
((e.equip_Quantity - eu.usage_Quantity)
+er.return_Quantity) IS NULL, e.equip_Quantity, (( e.equip_Quantity - eu.usage_Quantity)+er.return_Quantity))) available,
(eu.usage_Quantity - er.return_Quantity) usage_Quantity,
er.return_Quantity ,
eu.usage_ID
FROM `equipment` e
INNER JOIN unit u ON u.unit_ID = e.unit_ID
LEFT JOIN equipment_usage eu ON eu.equip_ID = e.equip_ID
LEFT JOIN equipment_return er ON er.usage_ID = eu.usage_ID
预期输出:
所有设备将被取回然后在列中包括可用设备,使用设备,返回设备,我的配方想法显示此表是:
total_equipment as total_quantity,
(total_equipment - total_usage of equipment) as available,
total_usage of equipment,
total_return of equipment
接受所有形式的查询只是为了输出此预期表提前感谢。 :)
答案 0 :(得分:1)
要解决此问题,您需要在进行连接之前聚合。它看起来像这样:
SELECT e.equip_ID, e.equip_Name,u.unit_Name,
COALESCE(eu.usage_quantity, 0) as usage_quantity,
COALESCE(er.return_quantity, 0) as return_quantity
FROM equipment e INNER JOIN
unit u
ON u.unit_ID = e.unit_ID LEFT JOIN
(SELECT eu.equip_id, SUM(eu.usage_Quantity) as usage_Quantity
FROM equipment_usage eu
GROUP BY eu.equip_ID
) eu
ON eu.equip_ID = e.equip_ID LEFT JOIN
(SELECT eu.equip_id, SUM(er.return_Quantity) as return_Quantity
FROM equipment_return er JOIN
equipment_usage eu
ON er.usage_ID = eu.usage_ID
GROUP BY eu.equip_id
) er
ON er.equip_ID = e.equip_ID;
Here是一个SQL小提琴。
答案 1 :(得分:1)
您不能在最终查询中包含usage_id,因为使用表中有多个equipment_id。在我计算可用设备之前,我先得到总和。请参阅此处的演示:http://sqlfiddle.com/#!9/d40bb9/2
SELECT equip_ID,
equip_Name,
unit_Name,
total_Quantity,
total_Quantity - coalesce(sum(total_usage),0) + coalesce(sum(total_return),0) as available,
sum(total_usage) as total_usage,
sum(total_return) as total_return
FROM (
SELECT
e.equip_ID,
e.equip_Name,
u.unit_Name,
e.equip_Quantity as total_quantity,
eu.usage_Quantity as total_usage,
sum(er.return_Quantity) as total_return,
eu.usage_ID
FROM `equipment` e
INNER JOIN unit u ON u.unit_ID = e.unit_ID
LEFT JOIN equipment_usage eu ON eu.equip_ID = e.equip_ID
LEFT JOIN equipment_return er ON er.usage_ID = eu.usage_ID
group by e.equip_ID,
e.equip_Name,
u.unit_Name,
e.equip_Quantity,
eu.usage_Quantity,
eu.usage_ID) t
group by equip_ID,
equip_Name,
unit_Name,
total_Quantity
Result:
equip_ID equip_Name unit_Name total_Quantity available total_usage total_return
13 Linemen Safety Belts set 20 20 (null) (null)
14 3tons Lever Block pc 30 24 13 7
15 1.5 tons Lever Block pc 10 1 10 1
16 3/4 tons Lever Block pc 1 1 1 1
17 5 tons Chain Block pc 1 0 1 (null)
18 3 tons Chain Block pc 1 1 (null) (null)
19 Linemen Tools (pliers, wrenches, hammers) lot 1 1 (null) (null)
20 Extendable Ladder 21 ft. Fiberglass pc 2 2 (null) (null)
21 Aluminum Ladder 10 ft. pc 2 2 (null) (null)