下面我有示例数据,我想将所有 biggest_win 和乘数合并到玩家1083987121的一列中!
select * from player;
player_key, country, biggest_win, multiplier, date
1083987121, USD, 490.2, 163.4, 2020-03-17
1083987121, USD, 256.0, 128.0, 2020-03-17
1083987121, USD, 210.0, 70.0, 2020-03-17
合并后的期望
1083987121, USD, 490.2#256.0#256.0 163.4#128.0#70.0, 2020-03-17
1083987121, USD, 490.2#256.0#256.0 163.4#128.0#70.0, 2020-03-17
1083987121, USD, 490.2#256.0#256.0 163.4#128.0#70.0, 2020-03-17
有人对此有好的解决方案吗?我已经尝试过XML函数,在BigQuery上似乎不起作用
答案 0 :(得分:1)
这回答了问题的原始版本。
select player_key, max(country) as country,
array_agg(biggest_win order by biggest_win desc),
array_agg(multiplier order by biggest_win desc),
max(date) as date
from t
group by player_key;
注意:这将返回数组而不是字符串中的值-数组更易于使用。但是,您也可以使用string_agg()
:
select player_key, max(country) as country,
string_agg(biggest_win, '#' order by biggest_win desc),
string_agg(multiplier, '#' order by biggest_win desc),
max(date) as date
from t
group by player_key, country;
答案 1 :(得分:1)
以下是BigQuery标准SQL
#standardSQL
SELECT player_key, country,
STRING_AGG(CAST(biggest_win AS STRING), '#') biggest_win,
STRING_AGG(CAST(multiplier AS STRING), '#') multiplier,
date
FROM `project.dataset.player`
GROUP BY player_key, country, date
有输出
Row player_key country biggest_win multiplier date
1 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17
注意:根据您的I'm expecting after merge
示例-我假设您希望合并后的结果-玩家,国家/地区和日期
我想知道是否要打印所有行
如果您仍要输出所有行(在其中看不到太多的值,但请按您的要求),请在下面使用
#standardSQL
SELECT player_key, country,
STRING_AGG(CAST(biggest_win AS STRING), '#') OVER(PARTITION BY player_key, country, date) biggest_win,
STRING_AGG(CAST(multiplier AS STRING), '#') OVER(PARTITION BY player_key, country, date) multiplier,
date
FROM `project.dataset.player`
在这种情况下,相应的输出是
Row player_key country biggest_win multiplier date
1 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17
2 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17
3 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17
要将这两列合并为一列-在下面使用
#standardSQL
SELECT player_key, country,
CONCAT(
STRING_AGG(CAST(biggest_win AS STRING), '#') OVER(PARTITION BY player_key, country, DATE), ' ',
STRING_AGG(CAST(multiplier AS STRING), '#') OVER(PARTITION BY player_key, country, DATE)
) combined_column,
DATE
FROM `project.dataset.player`
有输出
Row player_key country combined_column date
1 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17
2 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17
3 1083987121 USD 490.2#256#210 163.4#128#70 2020-03-17