首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >BigQuery SQL -如何将前3项结果转换为列

BigQuery SQL -如何将前3项结果转换为列
EN

Stack Overflow用户
提问于 2022-06-27 21:39:49
回答 1查看 43关注 0票数 1

长期潜伏的第一次海报..。

我有一张像下面这样的平坦的桌子,我想按事件把前三名的选手带回到不同的列中。

代码语言:javascript
运行
复制
with races as ( 
select "200M" as race, "johnson" as name,23.5 as finishtime
union all
select "200M" as race, "smith" as name,24.1 as finishtime
union all
select "200M" as race, "anderson" as name,23.9 as finishtime
union all
select "200M" as race, "jackson" as name,24.9 as finishtime
union all
select "400M" as race, "johnson" as name,47.1 as finishtime
union all
select "400M" as race, "alexander" as name,46.9 as finishtime
union all
select "400M" as race, "wise" as name,47.2 as finishtime
union all
select "400M" as race, "thompson" as name,46.8 as finishtime
    )

    select * from races

我希望输出基本上如下所示:

代码语言:javascript
运行
复制
Race  |  1st Place  |  2nd Place  | 3rd Place    
200M  |  johnson    |  anderson   | smith    
400M  |  thompson   |  alexander  | johnson

我没有在上面的数据中打成平局,但我也会有一些.

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2022-06-27 21:46:11

考虑以下几点

代码语言:javascript
运行
复制
select * from (
  select race, name, 
    row_number() over(partition by race order by finishtime) pos
  from races
)
pivot (any_value(name) as place for pos in (1,2,3))

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

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

https://stackoverflow.com/questions/72778572

复制
相关文章

相似问题

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