比较一个小组中的AVG行,所有行不在组中(BigQuery)

发布于 2025-01-22 16:51:24 字数 1903 浏览 0 评论 0原文

我有一个看起来像这样的数据集:

dategrp_nameuidvalue_avalue_bvalue_c
2022-01-01A11105
2022-01-01B27120
2022-01-01C107320
2022-01- 01A33124
2022-01-02B261 2121
2022-01-02B53419
2022-01-03A61156
2022-01-01-03C78222
2022-01- 03d910218

对于每个日期,每个grp_name,我想计算value_a,value_b and value_c and value_c copross as low as low lows and and(这是我遇到问题的地方):value_a,value_b and value_c的avg avg不在组中的行。

GRP_NAME的预期= a日期= 2022-01-01。我想生成一个IN_GRP列以将手头组与非组成员的平均值分开。

dategrp_namein_grpvalue_avalue_bvalue_c
2022-01-01atrue2114.5
2022-01-01afalse7220

这是我到目前为止写的简单查询,缺乏拾取非集团成员的能力对于平均值,并创建IN_GRP列以将组成员与非组成员分开:

SELECT
  date,
  grp_name,
  AVG(value_a) value_a,
  AVG(value_b) value_b,
  AVG(value_c) value_c
FROM table
GROUP BY date, grp_name

有关如何解决此问题的任何建议?

I have a data set that looks like this:

dategrp_nameuidvalue_avalue_bvalue_c
2022-01-01A11105
2022-01-01B27120
2022-01-01C107320
2022-01-01A33124
2022-01-02B26121
2022-01-02B53419
2022-01-03A61156
2022-01-03C78222
2022-01-03D910218

For each date, and each grp_name, I want to calculate the AVG of value_a, value_b and value_c accross all rows, and (here's where I run into problems): the AVG of value_a, value_b and value_c for all rows that are NOT in the group.

Expected for grp_name = A on date = 2022-01-01. I imagine generating an in_grp column to separate the average values that are from the group at hand from those that are from non-group members.

dategrp_namein_grpvalue_avalue_bvalue_c
2022-01-01ATRUE2114.5
2022-01-01AFALSE7220

Here is the simple query that I've written so far, that lacks the ability to pick up non-group members for the averages, and create the in_grp column to separate the group members from the non-group members:

SELECT
  date,
  grp_name,
  AVG(value_a) value_a,
  AVG(value_b) value_b,
  AVG(value_c) value_c
FROM table
GROUP BY date, grp_name

Any advice on how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-29 16:51:24

请考虑以下方法

with temp as (
  select distinct date, grp_name,
    count(*) over() count_all,
    count(*) over(partition by date, grp_name) count_in_grp,
    sum(value_a) over() sum_a,
    sum(value_a) over(partition by date, grp_name) sum_a_in_grp,
    sum(value_b) over() sum_b,
    sum(value_b) over(partition by date, grp_name) sum_b_in_grp,
    sum(value_c) over() sum_c,
    sum(value_c) over(partition by date, grp_name) sum_c_in_grp,
  from your_table
)
select date, grp_name, true as in_grp, 
  sum_a_in_grp / count_in_grp as value_a,
  sum_b_in_grp / count_in_grp as value_b,
  sum_c_in_grp / count_in_grp as value_c
from temp 
union all
select date, grp_name, false as in_grp, 
  (sum_a - sum_a_in_grp) / (count_all - count_in_grp) as value_a,
  (sum_b - sum_b_in_grp) / (count_all - count_in_grp) as value_b,
  (sum_c - sum_c_in_grp) / (count_all - count_in_grp) as value_c
from temp
-- order by date, grp_name, in_grp desc              

如果应用于您的问题输出中的样本数据,则为

”在此处输入图像描述”

Consider below approach

with temp as (
  select distinct date, grp_name,
    count(*) over() count_all,
    count(*) over(partition by date, grp_name) count_in_grp,
    sum(value_a) over() sum_a,
    sum(value_a) over(partition by date, grp_name) sum_a_in_grp,
    sum(value_b) over() sum_b,
    sum(value_b) over(partition by date, grp_name) sum_b_in_grp,
    sum(value_c) over() sum_c,
    sum(value_c) over(partition by date, grp_name) sum_c_in_grp,
  from your_table
)
select date, grp_name, true as in_grp, 
  sum_a_in_grp / count_in_grp as value_a,
  sum_b_in_grp / count_in_grp as value_b,
  sum_c_in_grp / count_in_grp as value_c
from temp 
union all
select date, grp_name, false as in_grp, 
  (sum_a - sum_a_in_grp) / (count_all - count_in_grp) as value_a,
  (sum_b - sum_b_in_grp) / (count_all - count_in_grp) as value_b,
  (sum_c - sum_c_in_grp) / (count_all - count_in_grp) as value_c
from temp
-- order by date, grp_name, in_grp desc              

if applied to sample data in your question output is

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文