0
    | Product ID | YearBought | Sales | Min_Year | Max_Year |
    |      1     |    2016    |  $20  |   2011   |   2016   |
    |      2     |    2016    |  $10  |   2016   |   2018   |
    |      2     |    2017    |  $30  |   2016   |   2018   |
    |      3     |    2017    |  $5   |   2015   |   2019   |
    |      3     |    2018    |  $10  |   2015   |   2019   |
    |      3     |    2018    |  $20  |   2015   |   2019   |
    |      3     |    2019    |  $5   |   2015   |   2019   |
    |      3     |    2019    |  $30  |   2015   |   2019   |
    |      4     |    2018    |  $5   |   2018   |   2020   |
    |      4     |    2019    |  $10  |   2018   |   2020   |
    |      4     |    2020    |  $20  |   2018   |   2020   |

Min_Year = 产品首次推出的年份

Max_Year + 1 = 产品下架年份

上面是我正在使用的表格的示例。试图找到:

  • 新产品首次推出当年带来的销售额总和

  • “销售额下降”的总和,即产品下降后一年的销售额总和(没有销售额)。(例如,产品在 2018 年带来了 15 美元,但在 2019 年没有销售额,希望在 2019 年销售额下降时显示 15 美元)

预期输出:

    |     YearBought   | New Product Sales | Dropped Product Sales | 
    |      2016        |        $10        |                       | 
    |      2017        |                   |           $20         |  
    |      2018        |         $5        |                       |  
    |      2019        |                   |                       |  
    |      2020        |                   |           $35         |   

正在考虑这样的事情,但它不起作用。任何帮助,将不胜感激!

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    
4

3 回答 3

0

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

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
于 2020-03-18T17:00:13.947 回答
0

将丢弃的产品销售与新产品销售分开聚合,然后加入聚合。您可以使用子查询来做到这一点,或者像我在下面所做的那样,使用公用表表达式。

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       |
+------------+-------+----------+---------------+
于 2020-03-18T20:55:52.477 回答
0

我认为你的问题是这些年是交错的。

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;
于 2020-03-18T21:26:23.270 回答