[英]BigQuery - Grouping by date with a WHERE clause
My goal is to search for certain products, count the total per certain product, and group each by day.我的目标是搜索某些产品,计算每种产品的总数,然后按天对每个产品进行分组。
Schema is like so:架构是这样的:
line_items.sku![]() |
created_at![]() |
---|---|
product1![]() |
2020-04-02T13:22:44 ![]() |
product2![]() |
2020-04-02T05:01:22 ![]() |
product2![]() |
2020-04-03T14:21:10 ![]() |
My query is below:我的查询如下:
SELECT
EXTRACT(DAY
FROM
CAST(`order`.created_at AS DATETIME)) AS day_extracted,
EXTRACT(MONTH
FROM
CAST(`order`.created_at AS DATETIME)) AS month_extracted,
`order`.line_items.sku AS sku
FROM
`mydatabase`
WHERE
`order`.line_items.sku = "product 1"
OR `order`.line_items.sku = "product 2"
The data is below:数据如下:
row![]() |
day_extracted ![]() |
month_extracted![]() |
sku![]() |
---|---|---|---|
1 ![]() |
5 ![]() |
2 ![]() |
product1![]() |
2 ![]() |
4 ![]() |
1 ![]() |
product2![]() |
2 ![]() |
4 ![]() |
1 ![]() |
product1![]() |
This is great and works, but I'm running into issues with needing to grouping the products and count per product total per day.这很好并且有效,但我遇到了需要对产品进行分组并每天计算每个产品总数的问题。
What am I doing wrong?我究竟做错了什么? If I add
如果我添加
GROUP BY month_extracted, day_extracted
to the query, another error comes up对于查询,出现另一个错误
SELECT list expression references `order`.line_items which is neither grouped nor aggregated at [8:3]
Line 8 is:第 8 行是:
`order`.line_items.sku AS sku
The order in which a general SQL query is evaluated is this评估一般 SQL 查询的顺序是这样的
Which means the group by clause doesn't even know what is month_extracted, day_extracted
.这意味着 group by 子句甚至不知道什么是
month_extracted, day_extracted
。 So in order to fix this, either put the whole exp EXTRACT(..
in Group by OR. Use a subquery. And also there is a rule, that anything in SELECT which is not part of GROUP BY should be applied an AGGREGATE function. In your it is not hence an error.因此,为了解决这个问题,要么将整个 exp
EXTRACT(..
放在 Group by OR 中。使用子查询。还有一条规则,即 SELECT 中不属于 GROUP BY 的任何内容都应应用 AGGREGATE function。因此,在您那里这不是错误。
select
day_extracted,
month_extracted,
any_value(sku) AS sku -- i used any_value to fix it, you can use any other agg. function as per your logic
from (
SELECT
EXTRACT(DAY
FROM
CAST(`order`.created_at AS DATETIME)) AS day_extracted,
EXTRACT(MONTH
FROM
CAST(`order`.created_at AS DATETIME)) AS month_extracted,
`order`.line_items.sku as Sku
FROM
`mydatabase`
WHERE
`order`.line_items.sku = "product 1"
OR `order`.line_items.sku = "product 2"
) as _table
group by day_extracted,month_extracted
Mr.Batra led me down the rabbit hole of subqueries and that led me to my solution. Mr.Batra 带我进入子查询的兔子洞,这让我找到了我的解决方案。 Knowing which order queries are executed in made more sense now too.
了解执行查询的顺序现在也更有意义了。
SELECT day_extracted,month_extracted,Sku,count(*) FROM
(
SELECT
EXTRACT(DAY
FROM
CAST(`order`.created_at AS DATETIME)) AS day_extracted,
EXTRACT(MONTH
FROM
CAST(`order`.created_at AS DATETIME)) AS month_extracted,
`order`.line_items.sku AS Sku
FROM
`mydatabase`
WHERE
`order`.line_items.sku = "product1"
OR `order`.line_items.sku = "product2"
) AS temp
GROUP BY temp.Sku,day_extracted,month_extracted
ORDER BY day_extracted
This gives me the data in this format:这给了我这种格式的数据:
day_extracted ![]() |
month_extracted![]() |
Sku![]() |
col1 ![]() |
---|---|---|---|
1 ![]() |
2 ![]() |
product1![]() |
41 ![]() |
1 ![]() |
2 ![]() |
product2![]() |
55 ![]() |
2 ![]() |
2 ![]() |
product1![]() |
91 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.