【问题标题】:Apply different Adjustment Factor for different date range对不同的日期范围应用不同的调整因子
【发布时间】:2019-09-19 01:23:29
【问题描述】:

我有一张表,其中包含不同日期的 SalesPerson 和 Sales。我有另一个表,其中有 SalesPerson 和需要应用不同调整因子的多个日期范围。

当调整因子表中有一行或没有一行时,我可以使用 case 语句来执行此操作。但我不确定如何为同一个销售人员循环不同的日期范围

    select a.date, a.salesperson, a.sales, b.adjustmentfactor, 
    case when a.date between b.startdate and b.enddate then b.adjustmentfactor 
    else 1 end realadjfactor,
    a.sales * case when a.date between b.startdate and b.enddate then 
    b.adjustmentfactor else 1 end realsales
    from sales a left join adjfactor b on a.salesperson = b.salesperson

【问题讨论】:

    标签: sql sql-server azure-sql-database azure-sql-server


    【解决方案1】:

    你只想要join吗?

    select s.date, s.salesperson, s.sales, 
           coalesce(af.realadjfactor, 1) * s.sales as adjustedsales,
           coalesce(af.realadjfactor, 1) as adjfactor
    from sales s left join
         adjfactor af
         on s.salesperson = af.salesperson and
            s.date between af.startdate and af.enddate;
    

    【讨论】:

    • 好吧,我需要找到特定日期的那个人的调整因子。该日期可以位于该人的任何行中,因此我需要从该行中获取该日期所在日期范围的调整因子;否则调整因子为1。请参考附图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多