首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >COUNTIF BIGQUERY

COUNTIF BIGQUERY
EN

Stack Overflow用户
提问于 2022-07-05 08:50:13
回答 2查看 157关注 0票数 0

在BigQuery中,我想知道每个球员中,谁赢得的分数最多,得分最少。

代码语言:javascript
复制
points_name_one  |points_name_two |name_one |name_two |concat_name  |   
---------------------------------------------------------------------
1                |  1             |Lou      |Max      |Lou - Max    |    
1                |  1             |Max      |Elie     |Max - Elie   |     
3                |  0             |Elie     |Zoe      |Elie - Zoe   |      
1                |  1             |Max      |Elie     |Max - Elie   |     
1                |  1             |Lou      |Max      |Lou - Max    |    
0                |  3             |Lou      |Max      |Lou - Max    |    
1                |  1             |Julien   |Max      |Julien - Max |         
1                |  1             |Max      |Elie     |Max - Elie   |     
3                |  0             |Elie     |Zoe      |Elie - Zoe   |      
0                |  3             |Zoe      |Lou      |Zoe - Lou    | 
3                |  0             |Lou      |Max      |Lou - Max    |    
1                |  1             |Max      |Elie     |Max - Elie   |     

我想我必须使用COUNTIF与over ( https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#countif ),但我尝试,但没有成功。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-05 09:23:27

如果我能很好地理解你的要求,下面的查询会向你的对手玩家展示你从谁那里得到的分数最多和最少。

代码语言:javascript
复制
WITH games AS (
  SELECT name_one AS player, name_two AS opponent, points_name_one AS point  FROM sample
   UNION ALL
  SELECT name_two, name_one, points_name_two FROM sample
)
SELECT player, opponent, SUM(point) AS points,
       FIRST_VALUE(opponent) OVER w AS least_from, 
       LAST_VALUE(opponent) OVER w AS most_from, 
  FROM games
 GROUP BY 1, 2
WINDOW w AS (PARTITION BY player ORDER BY SUM(point) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)  
;

输出:

例如,ElieMax得到4分,这是最少的,从Zoe得到6分,后者是最多的。

票数 1
EN

Stack Overflow用户

发布于 2022-07-05 18:03:46

还请考虑以下方法

代码语言:javascript
复制
select name_one as player, 
  array_agg(struct(name_two as most_from, points as most_points) order by points desc limit 1)[offset(0)].*,
  array_agg(struct(name_two as least_from, points as least_points) order by points limit 1)[offset(0)].* 
from (
  select name_one, name_two, sum(points_name_one) points
  from your_table group by 1,2 
  union all
  select name_two, name_one, sum(points_name_two) 
  from your_table group by 1,2
)
group by player  

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72866542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档