![](/img/trans.png)
[英]Bigquery: Select a column with any value not in group by clause
[英]Bigquery - Select a column with not grouping them in group by clause
我有基于 device_category(desktop/mobile/tablet) 和 user_type(new user/returning user) 拆分的谷歌分析数据的日表。
我的要求是,查询当月表现最好的产品,只知道设备和用户的类型。 我不想根据 device_category、user_type 对它们进行分组。
当从我的查询中排除它们时会出现错误提示 - “查询错误:SELECT 列表表达式引用列 device_category 既不在 [3:21] 分组也不聚合”
不工作的查询(这是我的要求)
SELECT
month,
year,
device_category,
user_type,
product_name,
round(sum(item_revenue),2) as item_revenue
FROM
`ProjectName.DatasetName.GA_REPORT_3_*`
where
_table_suffix between '20201101' and '20210131'
and channel_grouping = 'Organic Search'
group by
month,
year,
channel_grouping,
product_name
order by
item_revenue desc;
有效的查询
SELECT
month,
year,
device_category,
user_type,
product_name,
round(sum(item_revenue),2) as item_revenue
FROM
`ProjectName.DatasetName.GA_REPORT_3_*`
where
_table_suffix between '20201101' and '20210131'
and channel_grouping = 'Organic Search'
group by
month,
year,
channel_grouping,
product_name,
device_category,
user_type
order by
item_revenue desc;
样本数据
我知道在常规的 SQL 工作台中,我们可以 select 列在 SQL 中,而不是在 Group By 子句中。
你能帮我解决这个问题吗?
从技术上讲,您可以使用ANY_VALUE或MAX
或MIN
封装device_category
和user_type
:
SELECT
month,
year,
ANY_VALUE(device_category),
ANY_VALUE(user_type),
product_name,
round(sum(item_revenue),2) as item_revenue
FROM
`ProjectName.DatasetName.GA_REPORT_3_*`
where
_table_suffix between '20201101' and '20210131'
and channel_grouping = 'Organic Search'
group by
month,
year,
channel_grouping,
product_name
order by
item_revenue desc;
您可以使用子查询来实现此目的:
SELECT
x.month,
x.year,
x.device_category,
x.user_type,
x.product_name,
ROUND(SUM(x.item_revenue),2) as item_revenue
FROM
(SELECT
month,
year,
device_category,
user_type,
product_name,
item_revenue
FROM `ProjectName.DatasetName.GA_REPORT_3_*`
WHERE _table_suffix BETWEEN '20201101' and '20210131'
AND channel_grouping = 'Organic Search'
) x
GROUP BY
x.month,
x.year,
x.product_name,
x.device_category,
x.user_type
ORDER BY ROUND(SUM(x.item_revenue),2) DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.