你需要把所有的kpi
S IN的地圖,爆炸地圖創建一列,然後彙總。
實施例:
數據:
+---------+------+-----------+-------+-----+-----+------+------+------+------+
|day_ |class |start_time |count_ |kpi0 |kpi1 | kpi2 | kpi3 | kpi4 | kpi5 |
+---------+------+-----------+-------+-----+-----+------+------+------+------+
|20161010 |abc |00 |12 |1 |2 |3 |8 |9 |6 |
+---------+------+-----------+-------+-----+-----+------+------+------+------+
|20161010 |abc |00 |12 |4 |5 |null |6 |10 |null |
+---------+------+-----------+-------+-----+-----+------+------+------+------+
查詢:
SELECT day_
, class
, start_time
, count_
, kpi_type
, MAX(vals) AS max_vals
, MIN(vals) AS min_vals
, AVG(vals) AS avg_vals
FROM (
SELECT day_, class, start_time, count_, kpi_type, vals
FROM database.table
LATERAL VIEW EXPLODE(MAP('kpi0', kpi0
, 'kpi1', kpi1
, 'kpi2', kpi2
, 'kpi3', kpi3
, 'kpi4', kpi4
, 'kpi5', kpi5)) et AS kpi_type, vals) x
GROUP BY day_, class_, start_time, count_, kpi_type
輸出:
+---------+------+-----------+-------+---------+---------+---------+---------+
|day_ |class |start_time |count_ |kpi_type |max_vals |min_vals |avg_vals |
+---------+------+-----------+-------+---------+---------+---------+---------+
|20161010 |abc |00 |12 |kpi0 |4 |1 |2.5 |
+---------+------+-----------+-------+---------+---------+---------+---------+
|20161010 |abc |00 |12 |kpi1 |5 |2 |3.5 |
+---------+------+-----------+-------+---------+---------+---------+---------+
|20161010 |abc |00 |12 |kpi2 |3 |3 |3.0 |
+---------+------+-----------+-------+---------+---------+---------+---------+
|20161010 |abc |00 |12 |kpi3 |8 |6 |7.0 |
+---------+------+-----------+-------+---------+---------+---------+---------+
|20161010 |abc |00 |12 |kpi4 |10 |9 |9.5 |
+---------+------+-----------+-------+---------+---------+---------+---------+
|20161010 |abc |00 |12 |kpi5 |6 |6 |6.0 |
+---------+------+-----------+-------+---------+---------+---------+---------+
我以前的答案是不正確的。更新。 – gobrewers14
謝謝!解決方案爲我工作:) –