如果下个月没有数据,就获取上个月的数据

rmbxnbpk  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(317)

如果下个月没有数据,我想得到上个月的数据。假设数据为:

stock_item month stock
pepsi      4     100
pepsi      5     120
pepsi      9     80
coca cola  4     90
coca cola  6     100
coca cola  8     120

输出应如下所示:

stock_item month stock
pepsi      4     100
pepsi      5     120
pepsi      6     120
pepsi      7     120
pepsi      8     120
pepsi      9     80
pepsi      10    80
coca cola  4     90
coca cola  5     90
coca cola  6     100
coca cola  7     100
coca cola  8     120
coca cola  9     120
coca cola  10    120

月份在4到10之间,因为印度财政年度从4月份开始,当前月份是10月份,所以是10月份。

toe95027

toe950271#

这里有一个部分的答案,如何实现这一点,为一个单一的股票。
首先,为您想要的月份创建一个表并填充它。这给了我们一些可以重复的东西。

create table stock_months (
    int month_num not null
);
insert into stock_months values (4), (5), (6), (7), (8), (9), (10);

现在我们可以从这个表中选择月份 left outer joinstocks 得到所有的月份。

select stock_item, month_num, stock
from stock_months sm
left outer join stocks s
    on sm.month_num = s.month and s.stock_item = 'pepsi'

+------------+-----------+-------+
| stock_item | month_num | stock |
+------------+-----------+-------+
| pepsi      |         4 |   100 |
| pepsi      |         5 |   120 |
| pepsi      |         9 |    80 |
| NULL       |         6 |  NULL |
| NULL       |         7 |  NULL |
| NULL       |         8 |  NULL |
| NULL       |        10 |  NULL |
+------------+-----------+-------+

我们可以使用变量来记住前一行的值。

select
    case when stock_item is null then
        @stock_item
    else
        @stock_item := stock_item
    end as stock_item,
    month_num,
    case when stock is null then
        @stock
    else 
        @stock := stock
    end as stock
from stock_months sm
left outer join stocks s
    on sm.month_num = s.month and s.stock_item = 'pepsi'
order by month_num;

+------------+-----------+-------+
| stock_item | month_num | stock |
+------------+-----------+-------+
| pepsi      |         4 |   100 |
| pepsi      |         5 |   120 |
| pepsi      |         6 |    80 |
| pepsi      |         7 |    80 |
| pepsi      |         8 |    80 |
| pepsi      |         9 |    80 |
| pepsi      |        10 |    80 |
+------------+-----------+-------+

为什么没用?因为 select 按喜欢的顺序遍历联接的结果。然后分类。在本例中,它使用 stock_item 第一。
我们需要先对连接的行进行排序。我们可以从一个已经排序的子选择中进行选择。

select
    case when stock_item is null then
        @stock_item
    else
        @stock_item := stock_item
    end as stock_item,
    month_num,
    case when stock is null then
        @stock
    else 
        @stock := stock
    end as stock
from (
    select stock_item, month_num, stock
    from stock_months sm
    left outer join stocks s
        on sm.month_num = s.month and s.stock_item = 'pepsi'
    order by month_num
) stocks;

+------------+-----------+-------+
| stock_item | month_num | stock |
+------------+-----------+-------+
| pepsi      |         4 |   100 |
| pepsi      |         5 |   120 |
| pepsi      |         6 |   120 |
| pepsi      |         7 |   120 |
| pepsi      |         8 |   120 |
| pepsi      |         9 |    80 |
| pepsi      |        10 |    80 |
+------------+-----------+-------+

我不知道如何从这里到所有的库存项目。

相关问题