在T-SQL语句中计算SUM,该语句使用CASE输出值

时间:2018-06-07 16:57:04

标签: tsql sql-server-2008-r2

我有一张表格,其中包含已在电子商务网站上购买的订单,另一张表格详细列出了每张订单的产品(基本上是订单项)。我的职责是计算每种产品的总成本和总重量,按产品分组。店主希望能够识别按重量或价值出售最多的产品。

这两个表的结构如下:

顺序

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返回总值(在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和{{1}逻辑。

我想知道是否有人可以建议如何使这个工作,以便我可以返回正确的重量和价格总数?

非常感谢。

2 个答案:

答案 0 :(得分:1)

我无法对此进行测试,但您应该能够对原始查询进行子查询,并按逻辑将您的组应用于保持条件完整的总字段。

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

答案 1 :(得分:0)

根据@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