是否可以将 LAG() 与 WHERE 一起使用?

Rac*_*SQL 3 sql-server window-functions sql-server-2014

我们正在创建一个仓库系统。在这个系统中,我们将有一个提取页面(我不知道这个词是否适合描述它,但它与我们银行账户中的日志相同,我们在那里进行所有交易,带有值,然后,将其减去总价值,我猜这是银行提取物)。

我们需要一张像这样的表:

在此处输入图片说明

该表将包含来自财务票据的所有交易(包括产品的进入和退出)。如果您注意到,您可以看到ID1 将出现在位置 1、2 和 4。我确实尝试使用LAG,但使用该LAG函数,它获得了ID3 值。

如何获得滞后值,但具有所需的产品 ID?我需要totalPR_ID = 1的最后一个值。

我试过这个:

begin tran
insert into almoxarifado.Movimentacao
  (produto_id,documento,data,saldo_anterior,entradas,saidas)
select
Lotes.produto_id as produto_id,
NF.numero as documento,
nf.data_cadastro as data,
>>The lag value would be here, I need this field, to be the last total value,
>>but from this specific product.
Lotes.quantidade as entradas,
0 as saidas
from Almoxarifado.Entradas
join Compras.Notas_Fiscais NF on Entradas.nota_fiscal_id = NF.id
join Compras.Notas_Fiscais_Produtos NFP on NFP.nota_fiscal_id = NF.id
join Almoxarifado.Lotes on Lotes.nota_fiscal_produto_id = NFP.id 
 where convert(date,NF.data_cadastro) = '2017-01-10'
Run Code Online (Sandbox Code Playgroud)

每一行都需要有自己的总数。假设第 2 天,我们有 3 加仑水,总共给了我 10 加仑。然后,第 4 天我有 3 加仑,我需要总计 = 10 的行,在这个新行中,总计 13。

这就像银行摘录,您可以在其中查看您的费用以及每行的总额。因为,下一步将创建一个视图,该视图将按产品选择此表,因此,我可以看到一段时间内的每个总数、每个条目和产品退出。

McN*_*ets 5

如果这能解决您的问题,请告诉我。

我已经建立了一个rextester例子来测试它。

我用过这个数据:

create table #mov(produto_id int, documento int, data_cadastro datetime, entradas int, salidas int);

insert into #mov values
(1, 1, '2017-01-01', 10, 0),
(1, 1, '2017-01-02', 10, 2),
(2, 1, '2017-01-01', 10, 0),
(1, 1, '2017-01-03', 10, 0),
(3, 1, '2017-01-02', 10, 0);
Run Code Online (Sandbox Code Playgroud)

要获得累积总和,请SUM() OVER按以下方式应用子句:

select 
    produto_id, 
    documento,
    entradas,
    salidas,
    sum(entradas-salidas) over (partition by produto_id order by produto_id, data_cadastro
    rows between unbounded preceding and current row) as acumulato
from
    #mov
Run Code Online (Sandbox Code Playgroud)

这是结果:

+------------+-----------+----------+---------+-----------+
| produto_id | documento | entradas | salidas | acumulato |
+------------+-----------+----------+---------+-----------+
|      1     |     1     |    10    |    0    |     10    |
+------------+-----------+----------+---------+-----------+
|      1     |     1     |     0    |    2    |     8     |
+------------+-----------+----------+---------+-----------+
|      1     |     1     |    10    |    0    |     18    |
+------------+-----------+----------+---------+-----------+
|      2     |     1     |    12    |    0    |     12    |
+------------+-----------+----------+---------+-----------+
|      3     |     1     |     5    |    0    |     5     |
+------------+-----------+----------+---------+-----------+
Run Code Online (Sandbox Code Playgroud)

如果您需要同一行中的最后一个 acumulato,则可以使用:

;with cteSum as
(
    select 
        produto_id,
        data_cadastro,
        documento,
        entradas,
        salidas,
        sum(entradas-salidas) over (partition by produto_id order by produto_id, data_cadastro
        rows between unbounded preceding and current row) as acumulato
    from
        #mov
)
select produto_id, documento, entradas, salidas, acumulado, 
       lag(acumulato,1,0) over (partition by produto_id order by produto_id, data_cadastro) as last_acm
from cteSum;

+------------+-----------+----------+---------+-----------+----------+
| produto_id | documento | entradas | salidas | acumulato | last_acm |
+------------+-----------+----------+---------+-----------+----------+
|      1     |     1     |    10    |    0    |     10    |     0    |
+------------+-----------+----------+---------+-----------+----------+
|      1     |     1     |     0    |    2    |     8     |    10    |
+------------+-----------+----------+---------+-----------+----------+
|      1     |     1     |    10    |    0    |     18    |     8    |
+------------+-----------+----------+---------+-----------+----------+
|      2     |     1     |    12    |    0    |     12    |     0    |
+------------+-----------+----------+---------+-----------+----------+
|      3     |     1     |     5    |    0    |     5     |     0    |
+------------+-----------+----------+---------+-----------+----------+
Run Code Online (Sandbox Code Playgroud)