bigquery中按日期累加多个条件
我需要对每个类别、sub_category1、sub_category2 的 sales
表中的销售数量进行运行总计,例如:
日期 | 类别 | sub_category1 | sub_category2 | 数量 |
---|---|---|---|---|
01-01-2022 | 电子 | 计算机 | 笔记本电脑 | 2 |
01-01 -2022 | 电子 | 电脑 | 桌面 | 5 |
01-01-2022 | 电子 | 电脑 | 配件 | 10 |
01-01-2022 | 美容 | 沐浴 | 露 | 5 |
02-01-2022 | 电子 | 电脑 | 笔记本电脑 | 4 |
02-01-2022 | 电子 | 电脑 | 配件 | 3 |
02-01-2022 | 住房 | 厨房 | 用品 | 1 |
12-01-2022 | 美容 | 化妆品 | 口红 | 2 |
12-01 -2022 | 美容 | 沐浴沐浴 | 露 | 3 |
13-01-2022 | 电子 | 耳机 耳塞 | 式耳机 | 2 |
我的运行总计如下:
SELECT
* EXCEPT(quantity),
SUM(quantity) OVER (
PARTITION BY
category,
sub_category1,
sub_category2
ORDER BY date) AS running_total_quantity
FROM sales
我的结果应该是:
日期 | 类别 | sub_category1 | sub_category2 | running_total_quantity |
---|---|---|---|---|
01-01-2022 | 电子 | 电脑 | 笔记本电脑 | 2 |
01-01-2022 | 电子 | 电脑 | 台式机 | 5 |
01-01- 2022 | 电子 | 电脑 | 配件 | 10 |
01-01-2022 | 美容 | 沐浴及沐浴 | 露 | 5 |
02-01-2022 | 电子 | 电脑 | 笔记本电脑 | 6 |
02-01-2022 | 电子 | 电脑 | 配件 | 13 |
02-01-2022 | 住房 | 厨房 | 用品 | 1 |
12-01-2022 | 美容 | 化妆品 | 口红 | 2 |
12-01-2022 | 美容 | 沐浴及沐浴露沐浴 | 露 | 8 |
13-01-2022 | 电子 | 耳机 | Earbud headphone | 2 |
但是,当我创建条形图以在配置数据库上按周分组显示销售数量时,从 1 月 3 日到 1 月 9 日的一周时间没有任何内容,而我对表格的期望如下:
日期 | 类别 | sub_category1 | sub_category2 | running_total_quantity |
---|---|---|---|---|
01-01-2022 | 电子 | 电脑 | 笔记本电脑 | 2 |
01-01-2022 | 电子 | 电脑 | 台式机 | 5 |
01-01-2022 | 电子 | 电脑 | 配件 | 10 |
01-01-2022 | 美容 | 沐浴沐浴露 | 5 | 02-01-2022 |
电子 | 电脑 | 笔记本 | 电脑 | 6 |
02-01-2022 | 电子 | 电脑 | 台式机 | 5 |
02-01-2022 | 电子 | 电脑 | 配件 | 13 |
02-01-2022 | 美容 | 沐浴沐浴 | 露 | 5 |
02-01-2022 | 住房 | 厨房 | 电器 | 1 |
03-01-2022 | 电子 | 电脑 | 笔记本电脑 | 6 |
03-01-2022 | 电子 | 电脑 | 台式机 | 5 |
03-01-2022 | 电子 | 电脑 | 配件 | 13 |
03-01-2022 | 美容 | 沐浴沐浴 | 露 | 5 |
03-01-2022 | 住房 | 厨房 | 用具 | 1 |
... | ... | ... | ... | ... |
11-01-2022 | 电子 | 电脑 | 笔记本电脑 | 6 |
11-01-2022 | 电子 | 电脑 | 桌面 | 5 |
11-01-2022 | 电子 | 电脑 | 配件 | 13 |
11-01-2022 | 美容 | 沐浴及沐浴 | 露 | 5 |
11-01-2022 | 住房 | 厨房 | 用品 | 1 |
12-01-2022 | 电子 | 电脑 | 笔记本电脑 | 6 |
12-01 -2022 | 电子 | 电脑 | 桌面 | 5 |
12-01-2022 | 电子 | 电脑 | 配件 | 13 |
12-01-2022 | 美容 | 沐浴 | 露 | 8 |
12-01-2022 | 住房 | 厨房 | 用品 | 1 |
12-01-2022 | 美容 | 化妆品 | 口红 | 2 |
13-01-2022 | 电子 | 电脑 | 笔记本电脑 | 6 |
13-01-2022 | 电子 | 电脑 | 台式机 | 5 |
13-01-2022 | 电子 | 电脑 | 配件 | 13 |
13-01-2022 | 美容 | 沐浴露和沐浴 | 露 | 8 |
13-01-2022 | 住房 | 厨房 | 用品 | 1 |
13-01-2022 | 美容 | 化妆品 | 口红 | 2 |
13-01-2022 | 电子 | 耳机 | 耳塞式耳机 | 2 |
我用来创建一个生成所有日期的模型,类别,子类别1和子类别2然后使用左连接来填写。但是日期,类别等等都是无限的然后就不起作用了。
我该怎么做?
I need to do running total for the quantity of sales in the sales
table of each category, sub_category1, sub_category2 for example below:
date | category | sub_category1 | sub_category2 | quantity |
---|---|---|---|---|
01-01-2022 | Electronic | Computer | Laptop | 2 |
01-01-2022 | Electronic | Computer | Desktop | 5 |
01-01-2022 | Electronic | Computer | Accessories | 10 |
01-01-2022 | Beauty | Bath and Body | Shampoo | 5 |
02-01-2022 | Electronic | Computer | Laptop | 4 |
02-01-2022 | Electronic | Computer | Accessories | 3 |
02-01-2022 | Housing | Kitchen | Applicancies | 1 |
12-01-2022 | Beauty | Comestics | Lipstick | 2 |
12-01-2022 | Beauty | Bath and Body | Shampoo | 3 |
13-01-2022 | Electronic | Heaphones | Earbud headphone | 2 |
I do the running total as below:
SELECT
* EXCEPT(quantity),
SUM(quantity) OVER (
PARTITION BY
category,
sub_category1,
sub_category2
ORDER BY date) AS running_total_quantity
FROM sales
My result should be:
date | category | sub_category1 | sub_category2 | running_total_quantity |
---|---|---|---|---|
01-01-2022 | Electronic | Computer | Laptop | 2 |
01-01-2022 | Electronic | Computer | Desktop | 5 |
01-01-2022 | Electronic | Computer | Accessories | 10 |
01-01-2022 | Beauty | Bath and Body | Shampoo | 5 |
02-01-2022 | Electronic | Computer | Laptop | 6 |
02-01-2022 | Electronic | Computer | Accessories | 13 |
02-01-2022 | Housing | Kitchen | Applicancies | 1 |
12-01-2022 | Beauty | Comestics | Lipstick | 2 |
12-01-2022 | Beauty | Bath and Body | Shampoo | 8 |
13-01-2022 | Electronic | Heaphones | Earbud headphone | 2 |
However when I create bar chart to show sales quantity group by week on metabase, it is nothing for week time from 03 Jan to 09 Jan while my expectation of the table as below:
date | category | sub_category1 | sub_category2 | running_total_quantity |
---|---|---|---|---|
01-01-2022 | Electronic | Computer | Laptop | 2 |
01-01-2022 | Electronic | Computer | Desktop | 5 |
01-01-2022 | Electronic | Computer | Accessories | 10 |
01-01-2022 | Beauty | Bath and Body | Shampoo | 5 |
02-01-2022 | Electronic | Computer | Laptop | 6 |
02-01-2022 | Electronic | Computer | Desktop | 5 |
02-01-2022 | Electronic | Computer | Accessories | 13 |
02-01-2022 | Beauty | Bath and Body | Shampoo | 5 |
02-01-2022 | Housing | Kitchen | Applicancies | 1 |
03-01-2022 | Electronic | Computer | Laptop | 6 |
03-01-2022 | Electronic | Computer | Desktop | 5 |
03-01-2022 | Electronic | Computer | Accessories | 13 |
03-01-2022 | Beauty | Bath and Body | Shampoo | 5 |
03-01-2022 | Housing | Kitchen | Applicancies | 1 |
... | ... | ... | ... | ... |
11-01-2022 | Electronic | Computer | Laptop | 6 |
11-01-2022 | Electronic | Computer | Desktop | 5 |
11-01-2022 | Electronic | Computer | Accessories | 13 |
11-01-2022 | Beauty | Bath and Body | Shampoo | 5 |
11-01-2022 | Housing | Kitchen | Applicancies | 1 |
12-01-2022 | Electronic | Computer | Laptop | 6 |
12-01-2022 | Electronic | Computer | Desktop | 5 |
12-01-2022 | Electronic | Computer | Accessories | 13 |
12-01-2022 | Beauty | Bath and Body | Shampoo | 8 |
12-01-2022 | Housing | Kitchen | Applicancies | 1 |
12-01-2022 | Beauty | Comestics | Lipstick | 2 |
13-01-2022 | Electronic | Computer | Laptop | 6 |
13-01-2022 | Electronic | Computer | Desktop | 5 |
13-01-2022 | Electronic | Computer | Accessories | 13 |
13-01-2022 | Beauty | Bath and Body | Shampoo | 8 |
13-01-2022 | Housing | Kitchen | Applicancies | 1 |
13-01-2022 | Beauty | Comestics | Lipstick | 2 |
13-01-2022 | Electronic | Heaphones | Earbud headphone | 2 |
I used to create a model which generates all date, category, sub_category1 and subcategory2 then using left join to fill in. But the dates, category and so on are going unlimited then it didn't work.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论