Database 如何为特定类别的每月总销售额添加条件

Database 如何为特定类别的每月总销售额添加条件,database,oracle,subquery,case-when,Database,Oracle,Subquery,Case When,让我们假设有一个如下结构的表 select Year, Month , customer, Category, Amount from claim 我想为每个月申请折扣,如表中所示(绿色列): 如果1月份品牌和通用类别的“Cus X”总销售额大于15000,则仅对“品牌”类别应用2%的折扣 如果大于20000,则应用3%的折扣 其他月份和客户也是如此 在SQL中使用子查询或一些有帮助的函数可以做到这一点吗 请告知 您可以使用case。。当语句和分析功能如下: select Year, M

让我们假设有一个如下结构的表

select Year, Month , customer, Category, Amount 
from claim
我想为每个月申请折扣,如表中所示(绿色列):

  • 如果1月份品牌和通用类别的“Cus X”总销售额大于15000,则仅对“品牌”类别应用2%的折扣
  • 如果大于20000,则应用3%的折扣
其他月份和客户也是如此

在SQL中使用子查询或一些有帮助的函数可以做到这一点吗

请告知


您可以使用c
ase。。当
语句和分析功能如下:

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)

对于custY,月=2,年=2020,该客户当月的总销售额为13900,不应该有任何折扣。很好,这正是我想要的。谢谢您的帮助,谢谢您的评论。你说得对,第二位顾客不打折