如何为特定类别的每月总销售额添加条件
How can I add condition for total sales per month for specific category
假设有一个table如下结构
select Year, Month , customer, Category, Amount
from claim
我想为每个月应用折扣,如 table(绿色列)所示:
- 如果 1 月份 'Cus X' 品牌和通用类别的总销售额大于 15000,则仅对 'Brand' 类别应用 2% 的折扣
- 如果大于 20000 则应用 3% 的折扣
其他月份和客户也是如此。
是否可以在 SQL 中使用子查询或一些可以提供帮助的函数来做到这一点?
请指教
可以使用case .. when
语句和解析函数如下:
select Year, Month , customer, Category, Amount,
Case when category = 'Brand'
then
Case when total_sales > 15000 then '2%'
When total_sales > 20000 then '3%'
End
End as disc,
Case when category = 'Brand'
then
Case when total_sales > 15000 then 2*amount/100
When total_sales > 20000 then 3*amount/100
End
End disc_amount
From
(select Year, Month , customer, Category, Amount,
sum(case when category in ('Brand', 'Generic') then amount else 0 end)
over (partition by year, month, customer) as total_sales
from claim)
假设有一个table如下结构
select Year, Month , customer, Category, Amount
from claim
我想为每个月应用折扣,如 table(绿色列)所示:
- 如果 1 月份 'Cus X' 品牌和通用类别的总销售额大于 15000,则仅对 'Brand' 类别应用 2% 的折扣
- 如果大于 20000 则应用 3% 的折扣
其他月份和客户也是如此。
是否可以在 SQL 中使用子查询或一些可以提供帮助的函数来做到这一点?
请指教
可以使用case .. when
语句和解析函数如下:
select Year, Month , customer, Category, Amount,
Case when category = 'Brand'
then
Case when total_sales > 15000 then '2%'
When total_sales > 20000 then '3%'
End
End as disc,
Case when category = 'Brand'
then
Case when total_sales > 15000 then 2*amount/100
When total_sales > 20000 then 3*amount/100
End
End disc_amount
From
(select Year, Month , customer, Category, Amount,
sum(case when category in ('Brand', 'Generic') then amount else 0 end)
over (partition by year, month, customer) as total_sales
from claim)