我试图编写一个SQL语句,该语句将占用该位置的第一个字母(作为过道)并将该容量仅用于该字母
另一个问题是我正在加入一个表格,其中每个位置为该位置的每个股票多次列出。
...编辑 好吧好像我需要总结不同位置的容量? (不是明显的容量)
到目前为止SELECT
SUBSTRING(table_loc.Location,1,1) as 'Aisle',
SUM(table_loc.Capacity) as 'Aisle Capacity',
SUM(table_stock.LocationQTY) as 'Units in aisle'
FROM table_loc
INNER JOIN table_stock
ON table_loc.Location = table_stock.Location
GROUP BY SUBSTRING(table_loc.Location,1,1)
Location Capacity
A001 3
A002 2
B001 2
B002 2
Item Location LocationQTY
ShirtA A001 1
ShirtB A001 1
PantsA A002 1
PantsB A002 1
WidgetA B001 1
WidgetB B001 1
HatA B002 1
HatB B002 1
目前我的搜索结果与每个"项目"相乘在该位置
Location Capacity LocationQTY
A 13 8
B 8 8
Location Capacity LocationQTY
A 5 4
B 4 4
希望我没有过分混淆。
再次感谢
答案 0 :(得分:3)
你需要按Aisle分组。
修改强>
http://sqlfiddle.com/#!9/0ec44/2
因为您需要在第二个表中对一列进行求和,所以您需要执行以下操作:
Select Left(table_loc.Location, 1) As Aisle, Sum(table_loc.Capacity) As Aisle_Capacity, table_stock.Units_In_Aisle
From table_loc
Inner Join (
Select Left(table_stock.Location, 1) As Aisle, Sum(table_stock.LocationQTY) As Units_In_Aisle
From table_stock
Group By Aisle) As table_stock On (Left(table_loc.Location, 1) = table_stock.Aisle)
Group By Aisle
答案 1 :(得分:0)
首先,我会使用left
。其次,不需要distinct
:
SELECT LEFT(table.Location, 1) as Aisle,
SUM(table.Capacity) as AisleCapacity
FROM t
GROUP BY LEFT(table.Location, 1);
注意:不要对列别名使用单引号。这可能会在将来造成混乱。大多数列别名不需要转义。如果要在名称中使用奇怪的字符(如空格),请为数据库使用正确的转义字符(反引号,双引号或方括号)。