比较上下两条数据,多个字段多个条件,赋值一个结果字段成一个array,
WITH tmp AS (
SELECT 1 AS id,
1 AS c1,
1 AS c2
UNION ALL
SELECT 2 AS id,
2 AS c1,
2 AS c2
UNION ALL
SELECT 3 AS id,
1 AS c1,
2 AS c2
UNION ALL
SELECT 4 AS id,
2 AS c1,
1 AS c2
UNION ALL
SELECT 5 AS id,
2 AS c1,
2 AS c2
),
r1 AS (
SELECT row_num,
ARRAY(
CASE WHEN (c1 = 2 AND prev_c1 = 1) THEN 'a'
ELSE NULL
END,
CASE WHEN (c2 = 2 AND prev_c2 = 1) THEN 'b'
ELSE NULL
END
) AS event
FROM (
SELECT c1,
c2,
ROW_NUMBER() OVER (
ORDER BY
id
) AS row_num,
LAG(c1) OVER (
ORDER BY
id
) AS prev_c1,
LAG(c2) OVER (
ORDER BY
id
) AS prev_c2
FROM tmp
) ranked_data
),
r2 AS (
SELECT row_num,
exploded_event
FROM r1
LATERAL VIEW
EXPLODE(event) exploded AS exploded_event
)
SELECT * from r2 where exploded_event is not null;