0

我有这个查询,它在我的每个分支中返回 qty。现在该分支有两个 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;

作为参考,请参阅这个问题:如何对三列数据使用“分组依据”?

更新 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 回答