根据不同年份的案例总结销售额

Summing Sales Based On Different Year Cases

    | Product ID | YearBought | Sales | Min_Year | Max_Year |
    |      1     |    2016    |    |   2011   |   2016   |
    |      2     |    2016    |    |   2016   |   2018   |
    |      2     |    2017    |    |   2016   |   2018   |
    |      3     |    2017    |     |   2015   |   2019   |
    |      3     |    2018    |    |   2015   |   2019   |
    |      3     |    2018    |    |   2015   |   2019   |
    |      3     |    2019    |     |   2015   |   2019   |
    |      3     |    2019    |    |   2015   |   2019   |
    |      4     |    2018    |     |   2018   |   2020   |
    |      4     |    2019    |    |   2018   |   2020   |
    |      4     |    2020    |    |   2018   |   2020   |

Min_Year = 产品首次推出的年份

Max_Year + 1 = 产品流失年份

以上是我正在使用的 table 的示例。正在寻找:

预期输出:

    |     YearBought   | New Product Sales | Dropped Product Sales | 
    |      2016        |                |                       | 
    |      2017        |                   |                    |  
    |      2018        |                 |                       |  
    |      2019        |                   |                       |  
    |      2020        |                   |                    |   

正在考虑类似的事情,但它不起作用。如有任何帮助,我们将不胜感激!

select 
    YearBought,
    sum(case when yearbought=min_year then sales else 0 end) as NewSales,
    sum(case when yearbought=max_year+1 then sales else 0 end) as DropSales
from 
    #t 
group by 
    yearbought    

您可以先列出日期,然后将其与原始 table 合并并进行条件聚合:

select
    y.yearbought,
    sum(case when t.yearbought = t.min_year then sales end) new_product_sales,
    sum(case when t.yearbought = t.max_year then sales end) dropped_product_sales
from (select distinct yearbought from #t) y
inner join #t on y.yearbought in (t.min_year, t.max_year + 1)
group by y.yearbought

分别从新产品销售额中聚合掉落的产品销售额,然后加入聚合。您可以使用子查询来执行此操作,或者像我在下面所做的那样,使用常见的 table 表达式。

with

    droppedProds as (

        select      droppedYear = yearBought + 1, 
                    foregoneSales = sum(sales)
        from        @t t 
        where       YearBought = Max_Year
        group by    YearBought

    ),

    newSales as (

        select      YearBought,
                    sales = sum(sales),
                    newSales = sum(case when yearBought = min_year then sales end)
        from        @t t
        group by    YearBought

    )

    select      YearBought,
                n.sales,
                n.newSales,
                d.foregoneSales
    from        newSales n
    left join   droppedProds d on n.yearBought = d.droppedYear
    order by    YearBought;

结果:

+------------+-------+----------+---------------+
| YearBought | sales | newSales | foregoneSales |
|------------|-------|----------|---------------|
|    2016    |  30   |    10    |               |  
|    2017    |  35   |          |      20       |  
|    2018    |  35   |     5    |               |  
|    2019    |  45   |          |               |  
|    2020    |  20   |          |      35       |
+------------+-------+----------+---------------+

我觉得你的问题是年代错了

with n as (
    select year_bought as y, sum(sales) as sales,
        sum(case when year_bought = min_year then sales end) as ns
    from T group by year_bought
), d as (
    select year_bought + 1 as y,
        sum(case when year_bought = max_year then sales end) as ds
    from T group by year_bought
)
select y, sales,
    ns as newSales, coalesce(dropped, 0) as foregoneSales
from n left outer join p on p.y = n.y;

或使用lead()

select year_bought, sum(Sales) as Sales,
    sum(case when year_bought = min_year then sales end) as newSales,
    coalesce(lead(sum(case when year_bought = max_year then sales end))
        over (order by year_bought), 0) as foregoneSales
from T
group by year_bought;