Unit_name 1

问题描述 投票:0回答:1

我有以下表格

1. 材料单位。

id | Unit_name
1  | Nos.
2  | lts

2. 材料表。

id | Material_name
1  | bricks
2  | Cement

3. Grn表。

id | material_id | qty | unit
1  | 1           | 100 |   1
2  | 2           | 500 |   1
3  | 2           | 100 |   1
4  | 1           | 200 |   1

4. 消耗表。

id | material_id | qty | unit
1  | 1           | 50  |   1
2  | 2           | 100 |   1

预期结果如下。

Material Name | Unit | Total Qty | Total Consumed Qty | Stock
Bricks        | Nos. | 300       | 50                 | 250
Cement        | Nos. | 600       | 100                | 500

因此,在上述结果中,总数量将从Grn表中获取,总消耗数量将从Consumption表中获取,而库存是两者的差额,应按物料名称分组。

下面的查询只得到总数量的结果,需要你的帮助才能得到消耗数量。

I am new to SQL and did tried but stuck at this point and need help.

Select sm.material_name as 'Material Name', mu.unit_name as 'Unit Name' , sum(g.qty) as 'Total Qty' from grn g
JOIN material_table.sm ON g.material_id = sm.id
JOIN material_unit.mu ON g.unit = mu.id

GROUP by material_name

sql join union-all sql-query-store
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.