Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite在每天计算的产品上运行的事务总数_Sqlite_Join_Window Functions_Recursive Query - Fatal编程技术网

SQLite在每天计算的产品上运行的事务总数

SQLite在每天计算的产品上运行的事务总数,sqlite,join,window-functions,recursive-query,Sqlite,Join,Window Functions,Recursive Query,我有一个表(餐具室)用作分类账,只有在发生交易时,才会随时间记录产品的添加和删除。我想得到每天记录的所有产品的运行总数。也许有更好的方法可以得到这个结果,但我不想改变这个表 我试图得到的结果就是这里表格中的结果。多谢各位 您可以交叉将递归CTE连接到表的不同乘积,然后左连接到表,以使用sum()窗口函数获取运行总和: with dates as ( select min(date) date from pantry union all select date(date, '+1 da

我有一个表(餐具室)用作分类账,只有在发生交易时,才会随时间记录产品的添加和删除。我想得到每天记录的所有产品的运行总数。也许有更好的方法可以得到这个结果,但我不想改变这个表

我试图得到的结果就是这里表格中的结果。多谢各位


您可以
交叉
将递归
CTE
连接到表的不同乘积,然后左连接到表,以使用
sum()
窗口函数获取运行总和:

with dates as (
  select min(date) date from pantry
  union all
  select date(date, '+1 day')
  from dates
  where date < (select max(date) from pantry)
)
select *
from (
  select d.date, p.produce,
        sum(t.transactions) over (partition by p.produce order by d.date) running_sum
  from dates d 
  cross join (select distinct produce from pantry) p
  left join pantry t on t.date = d.date and t.produce = p.produce
)
where running_sum is not null
order by date
> date       | produce | running_sum
> :--------- | :------ | ----------:
> 2020-01-01 | banana  |          50
> 2020-01-02 | banana  |          50
> 2020-01-03 | apple   |           5
> 2020-01-03 | banana  |          40
> 2020-01-04 | apple   |           5
> 2020-01-04 | banana  |          40
> 2020-01-05 | apple   |           5
> 2020-01-05 | banana  |          40
> 2020-01-06 | apple   |           5
> 2020-01-06 | banana  |          40
> 2020-01-07 | apple   |           3
> 2020-01-07 | banana  |          20
> 2020-01-07 | grapes  |         100
> 2020-01-08 | apple   |           3
> 2020-01-08 | banana  |          20
> 2020-01-08 | grapes  |         100
> 2020-01-09 | apple   |           3
> 2020-01-09 | banana  |          20
> 2020-01-09 | grapes  |          50
> 2020-01-10 | apple   |           3
> 2020-01-10 | banana  |          20
> 2020-01-10 | grapes  |          50
> 2020-01-11 | apple   |          -1
> 2020-01-11 | banana  |          20
> 2020-01-11 | grapes  |          50
> 2020-01-11 | melon   |           2