如何使用配置单元将列值分隔为不同的列

yvt65v4c  于 2021-06-27  发布在  Hive
关注(0)|答案(2)|浏览(358)

输入:

name year run
 1. a    2008 4
 2. a    2009 3
 3. a    2008 4
 4. b    2009 8
 5. b    2008 5

配置单元中的输出:

name 2008 2009
 1. a 8 3
 2. b 5 8
hgb9j2n6

hgb9j2n61#

固定年份:

select name,
       max(case when year=2008 then run end) as year_2008, 
       max(case when year=2009 then run end) as year_2009, 
       ... and so on
  from my_table
  group by name;

在配置单元中不可能动态生成这样的列,但是您可以先选择不同的年份,然后使用shell生成这个sql。

57hvy0tb

57hvy0tb2#

根据我的理解,你需要把每年的一些跑步数据转换成年份栏
你需要求和函数,而不是max

select
sum(case when year=2008 then run else 0 end) 2008_run,
sum(case when year=2009 then run else 0 end) 2009_run,
from table t1
group by name;

每年找出前五名得分手。

with table1 as
(
select name, sum(runs) as RunsPerYear, year from myTable group by name, year
)
table2 as
(
select name, year, RunsPerYear, dense_rank() over (partition by name, year order by RunsPerYear) as rnk from table2
)
select name, year, RunsPerYear from table2 where rnk<=5;

相关问题