我有一个如下的示例数据集
WITH S AS (
SELECT 1 AS ID, 1 AS ID2, 'A' AS CODE, 10 AS VALUE
UNION ALL
SELECT 2 AS ID, 1 AS ID2, 'A' AS CODE, 10 AS VALUE
UNION ALL
SELECT 3 AS ID, 2 AS ID2, 'A' AS CODE, 10 AS VALUE
UNION ALL
SELECT 4 AS ID, 2 AS ID2, 'B' AS CODE, 10 AS VALUE
UNION ALL
SELECT 5 AS ID, 3 AS ID2, 'B' AS CODE, 10 AS VALUE
UNION ALL
SELECT 6 AS ID, 3 AS ID2, 'C' AS CODE, 10 AS VALUE
),
S2 AS
(SELECT
ID2, CODE, SUM(VALUE) AS VALUE
FROM S
GROUP BY 1,2
)
SELECT ID2, ARRAY_AGG(STRUCT(CODE, VALUE)) FROM S2
GROUP BY 1
我需要获取输出中嵌入的ARRAY Elements的运行总数。 我当前和预期的输出如下:
Current Output:
Row ID2 f0_.CODE f0_.VALUE
1 3 C 10
B 10
2 2 B 10
A 10
3 1 A 20
Expected Output:
Row ID2 f0_.CODE f0_.VALUE f1_.CODE f1_VALUE
1 1 A 20 A 20
2 2 B 10 B 10
A 10 A 30
3 3 C 10 C 10
B 10 B 20
A 30
欣赏您的想法。
答案 0 :(得分:0)
#standardSQL
WITH S AS (
SELECT 1 AS ID, 1 AS ID2, 'A' AS CODE, 10 AS VALUE UNION ALL
SELECT 2 AS ID, 1 AS ID2, 'A' AS CODE, 10 AS VALUE UNION ALL
SELECT 3 AS ID, 2 AS ID2, 'A' AS CODE, 10 AS VALUE UNION ALL
SELECT 4 AS ID, 2 AS ID2, 'B' AS CODE, 10 AS VALUE UNION ALL
SELECT 5 AS ID, 3 AS ID2, 'B' AS CODE, 10 AS VALUE UNION ALL
SELECT 6 AS ID, 3 AS ID2, 'C' AS CODE, 10 AS VALUE
), S1 AS (
SELECT A.ID2, B.CODE, MAX(A.ID) ID
FROM S AS A CROSS JOIN S AS B
GROUP BY ID2, CODE
), S2 AS (
SELECT ID2, CODE, SUM(VALUE) AS VALUE, MAX(ID) ID
FROM S
GROUP BY ID2, CODE
), S3 AS (
SELECT S1.ID2, S1.CODE, IFNULL(S2.VALUE, 0) VALUE, IFNULL(S2.ID, S1.ID) ID
FROM S1 LEFT JOIN S2
USING(ID2, CODE)
), S4 AS (
SELECT ID2, CODE, VALUE, SUM(VALUE) OVER(PARTITION BY CODE ORDER BY ID) TOTAL
FROM S3
), S5 AS (
SELECT ID2, ARRAY_AGG(STRUCT(CODE, VALUE)) arr1, ARRAY_AGG(STRUCT(CODE, TOTAL)) arr2
FROM S4
GROUP BY ID2
)
SELECT ID2,
ARRAY(SELECT item FROM UNNEST(arr1) item WHERE item.VALUE != 0),
ARRAY(SELECT item FROM UNNEST(arr2) item WHERE item.TOTAL != 0)
FROM S5
ORDER BY ID2
给您
Row ID2 f0_.CODE f0_.VALUE f1_.CODE f1_.TOTAL
1 1 A 20 A 20
2 2 A 10 A 30
B 10 B 10
3 3 B 10 A 30
C 10 B 20
C 10