#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