如何在struct数组中的数组中排列元素?
with races as(
select "800m" race, "Rudisha" name,23.4 s1, 26.3 s2, 26.4 s3, 26.1 s4 union all
select "800m" race, "Makhloufi" name,24.5 , 25.4, 26.6, 26.1 union all
select "800m" race, "Murphy" name,23.9, 26.0, 27.0, 26.0 union all
select "800m" race, "Bosse" name,23.6, 26.2, 26.5, 27.1 union all
select "800m" race, "Rotich" name,24.7, 25.6, 26.9, 26.4 union all
select "800m" race, "Lewandowski" name,25.0, 25.7, 26.3, 27.2 union all
select "800m" race, "Kipketer" name,23.2, 26.1, 27.3, 29.4 union all
select "800m" race, "Berian" name,23.7, 26.1, 27.0, 29.3
)
我尝试了以下查询,但是没有用。
select race,array(select x splits from unnest(arr.splits)x order by x )from
(select race,[struct(name as name, [s1,s2,s3,s4] as splits)]arr from races)
答案 0 :(得分:2)
因此,您只需要在s1,s2,s3,s4列中排列值,以使它们在相同的s1,s2,s3,s4中排序? -是的,您是正确的
以下是用于BigQuery标准SQL
#standardSQL
SELECT
race,
name,
splits[OFFSET(0)] AS s1,
splits[OFFSET(1)] AS s2,
splits[OFFSET(2)] AS s3,
splits[OFFSET(3)] AS s4
FROM (
SELECT
race,
name,
ARRAY(SELECT s FROM UNNEST([s1, s2, s3, s4]) s ORDER BY s) splits
FROM `project.dataset.races`
)
您可以使用下面的问题中的虚拟数据进行测试,操作
#standardSQL
WITH `project.dataset.races` AS (
SELECT "800m" race, "Rudisha" name,23.4 s1, 26.3 s2, 26.4 s3, 26.1 s4 UNION ALL
SELECT "800m" race, "Makhloufi" name,24.5 , 25.4, 26.6, 26.1 UNION ALL
SELECT "800m" race, "Murphy" name,23.9, 26.0, 27.0, 26.0 UNION ALL
SELECT "800m" race, "Bosse" name,23.6, 26.2, 26.5, 27.1 UNION ALL
SELECT "800m" race, "Rotich" name,24.7, 25.6, 26.9, 26.4 UNION ALL
SELECT "800m" race, "Lewandowski" name,25.0, 25.7, 26.3, 27.2 UNION ALL
SELECT "800m" race, "Kipketer" name,23.2, 26.1, 27.3, 29.4 UNION ALL
SELECT "800m" race, "Berian" name,23.7, 26.1, 27.0, 29.3
)
SELECT
race,
name,
splits[OFFSET(0)] AS s1,
splits[OFFSET(1)] AS s2,
splits[OFFSET(2)] AS s3,
splits[OFFSET(3)] AS s4
FROM (
SELECT
race,
name,
ARRAY(SELECT s FROM UNNEST([s1, s2, s3, s4]) s ORDER BY s) splits
FROM `project.dataset.races`
)
结果/输出为
Row race name s1 s2 s3 s4
1 800m Murphy 23.9 26.0 26.0 27.0
2 800m Bosse 23.6 26.2 26.5 27.1
3 800m Lewandowski 25.0 25.7 26.3 27.2
4 800m Makhloufi 24.5 25.4 26.1 26.6
5 800m Rudisha 23.4 26.1 26.3 26.4
6 800m Kipketer 23.2 26.1 27.3 29.4
7 800m Berian 23.7 26.1 27.0 29.3
8 800m Rotich 24.7 25.6 26.4 26.9