2012-11-18 109 views
0

我有這個查詢返回每個我的分支數量。現在該分支有兩個WH_subType,如您在附圖中所看到的。我想總結2個子類型並顯示其可用數量。我該怎麼做。 enter image description here總計記錄數據到一個

我選擇查詢是這樣

SELECT 
dbo.WarehouseType.name      AS Section, 
dbo.WarehouseSubType.name      AS WH_Type, 
dbo.WarehouseSubType1.name     AS WH_SubType, 
dbo.Branch.name        AS Branch, 
(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity) AS AvailQty, 
dbo.WarehouseProductQuantity.tafsilId   AS Tafsil, 
dbo.Tafsil.description      AS Product_Name 
FROM 
dbo.WarehouseSubType 
INNER JOIN 
dbo.WarehouseType 
ON 
(
    dbo.WarehouseSubType.warehouseTypeId = dbo.WarehouseType.id) 
INNER JOIN 
dbo.WarehouseSubType1 
ON 
(
    dbo.WarehouseSubType.id = dbo.WarehouseSubType1.warehouseSubTypeId) 
INNER JOIN 
dbo.Warehouse 
ON 
(
    dbo.WarehouseSubType1.id = dbo.Warehouse.warehouseSubType1Id) 
INNER JOIN 
dbo.Branch 
ON 
(
    dbo.Warehouse.branchId = dbo.Branch.id) 
INNER JOIN 
dbo.WarehouseProductQuantity 
ON 
(
    dbo.Warehouse.id = dbo.WarehouseProductQuantity.warehouseId) 
INNER JOIN 
dbo.TafsilLink 
ON 
(
    dbo.WarehouseProductQuantity.tafsilId = dbo.TafsilLink.sourceId) 
INNER JOIN 
dbo.Tafsil 
ON 
(
    dbo.TafsilLink.targetId = dbo.Tafsil.id) 
INNER JOIN 
dbo.FinishProduct 
ON 
(
    dbo.Tafsil.id = dbo.FinishProduct.tafsilId) 
INNER JOIN 
dbo.Supplier 
ON 
(
    dbo.FinishProduct.supplierId = dbo.Supplier.tafsilId) 
WHERE 
WarehouseSubType1.warehouseSubTypeId IN (1,4) 
group by dbo.WarehouseProductQuantity.tafsilId 
+0

這是很難從你的問題弄清楚你想要什麼。因爲你沒有提供測試數據和期望的輸出。但是,看起來,你只需要'GROUP BY' http://msdn.microsoft.com/en-us/library/ms177673.aspx –

+0

@Igor我需要分組哪個列。 >>因爲您沒有提供測試數據 測試數據在圖片中。 >>和期望的輸出。 我想在同一分支列中添加產品列的WH_subtype列的AvailQty單元格。 – ZAJ

+0

@MahmoudGamal你的意思你桌子的其餘部分在哪裏? – ZAJ

回答

2

您是否嘗試過一組由

SELECT SubType, SUM(qty) AS QtySum 
GROUP BY SubType 

每個柱分組應該是你的選擇。注:每列你按它進一步子分割數據

更新基於OP評論: 如果你想其他列,你需要做的像

SELECT s.WH_SubType,s.AvailQty, t.other_cols 
from 
(SELECT 
dbo.WarehouseSubType1.name     AS WH_SubType, 
sum(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity) AS AvailQty 
FROM 
table 
GROUP BY 
dbo.WarehouseSubType1.name) s 
left join table t on t.dbo.WarehouseSubType1.name = s.WH_SubType; 

東西作爲參考看到這樣一個問題:How do I use "group by" with three columns of data?

更新2:

SELECT 
dbo.WarehouseType.name      AS Section, 
dbo.WarehouseSubType.name      AS WH_Type, 
dbo.WarehouseSubType1.name     AS WH_SubType, 
dbo.Branch.name        AS Branch, 
SumTable.AvailQty, 
SumTable.Tafsil, 
dbo.Tafsil.description      AS Product_Name 
FROM 
dbo.WarehouseSubType 
INNER JOIN 
dbo.WarehouseType 
ON 
(
    dbo.WarehouseSubType.warehouseTypeId = dbo.WarehouseType.id) 
INNER JOIN 
dbo.WarehouseSubType1 
ON 
(
    dbo.WarehouseSubType.id = dbo.WarehouseSubType1.warehouseSubTypeId) 
INNER JOIN 
dbo.Warehouse 
ON 
(
    dbo.WarehouseSubType1.id = dbo.Warehouse.warehouseSubType1Id) 
INNER JOIN 
dbo.Branch 
ON 
(
    dbo.Warehouse.branchId = dbo.Branch.id) 
INNER JOIN 
dbo.WarehouseProductQuantity 
ON 
(
    dbo.Warehouse.id = dbo.WarehouseProductQuantity.warehouseId) 
INNER JOIN 
dbo.TafsilLink 
ON 
(
    dbo.WarehouseProductQuantity.tafsilId = dbo.TafsilLink.sourceId) 
INNER JOIN 
dbo.Tafsil 
ON 
(
    dbo.TafsilLink.targetId = dbo.Tafsil.id) 
INNER JOIN 
dbo.FinishProduct 
ON 
(
    dbo.Tafsil.id = dbo.FinishProduct.tafsilId) 
LEFT JOIN (SELECT 
sum(dbo.WarehouseProductQuantity.actualQuantity - dbo.WarehouseProductQuantity.reservedQuantity) AS AvailQty, 
dbo.WarehouseProductQuantity.tafsilId   AS Tafsil 
FROM 
dbo.WarehouseProductQuantity 
group by dbo.WarehouseProductQuantity.tafsilId) SumTable on dbo.Tafsil.id = SumTable.Tafsil 
WHERE 
WarehouseSubType1.warehouseSubTypeId IN (1,4) 
+0

我已編輯我的問題,並添加完整的查詢,使事情更清晰。我按照您提到的方式添加了該組,但是我收到錯誤 「列'dbo.WarehouseType.name'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。」 – ZAJ

+0

看到我的更新 - 您需要做一個子查詢來完成總和,然後將其加入到原始表中獲取其他列 – Rob

+0

無法綁定多部分標識符「dbo.WarehouseProductQuantity.tafsilId」。 2)[錯誤代碼:4104,SQL狀態:S1000]無法綁定多部分標識符「dbo.WarehouseProductQuantity.actualQuantity」。 3)[錯誤代碼:4104,SQL狀態:S1000]無法綁定多部分標識符「dbo.WarehouseProductQuantity.reservedQuantity」。 4)[錯誤代碼:4104,SQL狀態:S1000]無法綁定多部分標識符「dbo.WarehouseProductQuantity.tafsilId」。 – ZAJ

0

你需要做的是這樣

SELECT SUM(AvailQty), ... FROM ... WHERE ... GROUP BY WH_SubType 

http://www.w3schools.com/sql/sql_func_sum.asp http://www.w3schools.com/sql/sql_groupby.asp

+0

我添加此SUM(AvailQty)在我的SELECT和GROUP BY我添加此GROUP BY dbo.Tafsil.id但我得到錯誤 「列'dbo.WarehouseType.name'在選擇列表中無效,因爲它是不包含在聚合函數或GROUP BY子句中2)[錯誤代碼:207,SQL狀態:42S22]無效的列名稱'AvailQty'。「 – ZAJ