0

各ブランチで数量を返すこのクエリがあります。これで、添付の図に示すように、ブランチには2つのWH_subTypeがあります。2つのサブタイプを合計して、使用可能な数量を表示したいと思います。どうすればいいですか。ここに画像の説明を入力してください

私の選択クエリは次のようになります

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
4

2 に答える 2

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;

参考までに、この質問を参照してください:3列のデータで「groupby」を使用するにはどうすればよいですか?

更新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)
于 2012-11-18T08:14:12.243 に答える
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

于 2012-11-18T08:18:53.757 に答える