在使用 CASE 输出值的 T-SQL 语句中计算 SUM
Calculate SUM in T-SQL statement which uses CASE to output values
我有一个 table 包含在电子商务网站上购买的订单,第二个 table 详细说明每个订单的产品(基本上是订单项)。我的职责是计算每个订购产品的总成本和总重量,按产品分组。店主希望能够按重量或价值确定销量最高的产品。
两个table的结构是这样的:
订单
id
orderDate
订单行
id
orderId
productNodeId
productName
quantity
duration
frequency
subscription
unitPrice
unitWeight
虽然有些订单行是简单的一次性产品(因此计算总数很简单,即总重量为 quantity * unitWeight
,总成本为 quantity * unitCost
),但其他订单行是订阅,其中总重量将按 (duration/frequency) * unitWeight
计算,总成本将是 (duration/frequency) * unitCost
.
我有以下 T-SQL 语句,它使用 CASE 来 return 总值 (运行 in SQL Server 2008R2 Express) :
SELECT
productNodeId AS id,
productName AS name,
quantity,
duration,
frequency,
subscription,
unitPrice,
CASE
WHEN subscription = 1 THEN unitWeight*(duration/frequency)
ELSE
unitWeight*quantity
END AS weight,
CASE
WHEN subscription = 1 THEN unitPrice*(duration/frequency)
ELSE
unitPrice*quantity
END AS total
FROM Order INNER JOIN OrderLine ON Order.id = OrderLine.orderId WHERE orderStatusId = 3
--GROUP BY productNodeId
ORDER BY total DESC
这在一定程度上有效。它输出以下值:
id | name | quantity | duration | frequency | subscription | unitPrice | weight | total
1105 Option C 1 24 1 1 2.50 18000 60.00
1105 Option C 1 24 2 1 2.50 9000 30.00
1104 Option B 1 24 2 1 2.50 9000 30.00
1105 Option C 1 24 2 1 2.50 10800 30.00
1105 Option C 1 4 1 1 2.50 2000 10.00
1107 Option A 1 0 0 0 2.80 600 2.80
1107 Option A 1 0 0 0 2.80 600 2.80
1121 Option D 1 0 0 0 2.80 400 2.80
不过,最后一部分是对总数求和。但是,我不能只执行上面代码中注释掉的 GROUP BY productNodeId
语句,因为它需要聚合 subscription
字段,这否定了条件 weight
和 total
逻辑。
我想知道是否有人可以建议如何让它工作,以便我可以 return 更正重量和价格的总和?
非常感谢。
我无法对此进行测试,但您应该能够对原始查询进行子查询,并按逻辑将您的分组应用于总字段,从而保持您的条件不变。
select id,name,quantity,duration,frequency,subscription,unitprice,weight,sum(total) as sumedtotal
from (
SELECT
productNodeId AS id,
productName AS name,
quantity,
duration,
frequency,
subscription,
unitPrice,
CASE
WHEN subscription = 1 THEN unitWeight*(duration/frequency)
ELSE
unitWeight*quantity
END AS weight,
CASE
WHEN subscription = 1 THEN unitPrice*(duration/frequency)
ELSE
unitPrice*quantity
END AS total
FROM Order INNER JOIN OrderLine ON Order.id = OrderLine.orderId WHERE orderStatusId = 3
--GROUP BY productNodeId
) as x
group by id,name,quantity,duration,frequency,subscription,unitprice,weight
根据@Anthony Hancock 的评论,最简单的解决方案是将 CASE
条件语句包装在 SUM
聚合器中,如下所示:
SELECT
productNodeId AS id,
MAX(productName) AS name,
SUM(CASE
WHEN subscription = 1 THEN unitWeight*(duration/frequency)
ELSE
unitWeight*quantity
END) AS weight,
SUM(CASE
WHEN subscription = 1 THEN unitPrice*(duration/frequency)
ELSE
unitPrice*quantity
END) AS total
FROM Order INNER JOIN OrderLine ON Order.id = OrderLine.orderId WHERE orderStatusId = 3
GROUP BY productNodeId
ORDER BY total DESC
我有一个 table 包含在电子商务网站上购买的订单,第二个 table 详细说明每个订单的产品(基本上是订单项)。我的职责是计算每个订购产品的总成本和总重量,按产品分组。店主希望能够按重量或价值确定销量最高的产品。
两个table的结构是这样的:
订单
id
orderDate
订单行
id
orderId
productNodeId
productName
quantity
duration
frequency
subscription
unitPrice
unitWeight
虽然有些订单行是简单的一次性产品(因此计算总数很简单,即总重量为 quantity * unitWeight
,总成本为 quantity * unitCost
),但其他订单行是订阅,其中总重量将按 (duration/frequency) * unitWeight
计算,总成本将是 (duration/frequency) * unitCost
.
我有以下 T-SQL 语句,它使用 CASE 来 return 总值 (运行 in SQL Server 2008R2 Express) :
SELECT
productNodeId AS id,
productName AS name,
quantity,
duration,
frequency,
subscription,
unitPrice,
CASE
WHEN subscription = 1 THEN unitWeight*(duration/frequency)
ELSE
unitWeight*quantity
END AS weight,
CASE
WHEN subscription = 1 THEN unitPrice*(duration/frequency)
ELSE
unitPrice*quantity
END AS total
FROM Order INNER JOIN OrderLine ON Order.id = OrderLine.orderId WHERE orderStatusId = 3
--GROUP BY productNodeId
ORDER BY total DESC
这在一定程度上有效。它输出以下值:
id | name | quantity | duration | frequency | subscription | unitPrice | weight | total
1105 Option C 1 24 1 1 2.50 18000 60.00
1105 Option C 1 24 2 1 2.50 9000 30.00
1104 Option B 1 24 2 1 2.50 9000 30.00
1105 Option C 1 24 2 1 2.50 10800 30.00
1105 Option C 1 4 1 1 2.50 2000 10.00
1107 Option A 1 0 0 0 2.80 600 2.80
1107 Option A 1 0 0 0 2.80 600 2.80
1121 Option D 1 0 0 0 2.80 400 2.80
不过,最后一部分是对总数求和。但是,我不能只执行上面代码中注释掉的 GROUP BY productNodeId
语句,因为它需要聚合 subscription
字段,这否定了条件 weight
和 total
逻辑。
我想知道是否有人可以建议如何让它工作,以便我可以 return 更正重量和价格的总和?
非常感谢。
我无法对此进行测试,但您应该能够对原始查询进行子查询,并按逻辑将您的分组应用于总字段,从而保持您的条件不变。
select id,name,quantity,duration,frequency,subscription,unitprice,weight,sum(total) as sumedtotal
from (
SELECT
productNodeId AS id,
productName AS name,
quantity,
duration,
frequency,
subscription,
unitPrice,
CASE
WHEN subscription = 1 THEN unitWeight*(duration/frequency)
ELSE
unitWeight*quantity
END AS weight,
CASE
WHEN subscription = 1 THEN unitPrice*(duration/frequency)
ELSE
unitPrice*quantity
END AS total
FROM Order INNER JOIN OrderLine ON Order.id = OrderLine.orderId WHERE orderStatusId = 3
--GROUP BY productNodeId
) as x
group by id,name,quantity,duration,frequency,subscription,unitprice,weight
根据@Anthony Hancock 的评论,最简单的解决方案是将 CASE
条件语句包装在 SUM
聚合器中,如下所示:
SELECT
productNodeId AS id,
MAX(productName) AS name,
SUM(CASE
WHEN subscription = 1 THEN unitWeight*(duration/frequency)
ELSE
unitWeight*quantity
END) AS weight,
SUM(CASE
WHEN subscription = 1 THEN unitPrice*(duration/frequency)
ELSE
unitPrice*quantity
END) AS total
FROM Order INNER JOIN OrderLine ON Order.id = OrderLine.orderId WHERE orderStatusId = 3
GROUP BY productNodeId
ORDER BY total DESC