我有Orderdetails表,其中包含订单详细信息,我需要计算每个产品的退款(退回)订单减去每个产品的销售额。
ex: total sales of product = sales orders total - returned orders of a product
注意:我使用了交易类型字段,以便我可以过滤订单类型。
订单明细表字段。
ID - Productname - Price - ono - transaction_type
11 - prod1 - 100.00- 1 - salesorder
12 - prod2 - 200.00- 2 - salesorder
13 - prod1 - 100.00- 1 - returned
14 - prod2 - 200.00- 3 - salesorder
15 - prod2 - 200.00- 4 - salesorder
我要求的是什么
Productname - Total sales
Prod1 - 0
Prod2 - 3
答案 0 :(得分:0)
你可以这样试试---
Select Product_NAME, Sum(Case when transaction_type='salesorder' then 1 Else 0 End)- Sum(Case when transaction_type!='salesorder' then 1 Else 0 End) as TotalSale
from OrderDetals group by Product_NAME
答案 1 :(得分:0)
试试这个!
SELECT ProductName,
SUM(CASE WHEN [transaction_type] = 'salesorder' THEN [Quentity] ELSE -1 * [Quentity] END) AS [Total Number Of Sale]
FROM your_table)name
GROUP BY ProductName