10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Prestoでhistogram関数を使って可視化したいときのクエリ(on TreasureData)

Last updated at Posted at 2015-08-14

Prestoには、Histgram関数がありますが、返値がMap型になっています。
なので、その結果をどこかに書き出したいと思ってもイマイチ使いづらいです。

SELECT 
  method,
  histogram(code) AS hist
FROM
  www_access
GROUP BY
  method

result1

こうしたときには、Mapの値を展開して、フラっトな形に直したいです。
そのためには、下記の関数を使います。

そんでもって、下のようなクエリを書きます。

With句のhist_tableでヒストグラムを作って、split_tableでそれらをkeyとvalueで分けて、最後にUnnestで展開してます。

WITH hist_table AS (
  select method, histogram(code) as hist
  from www_access group by method
),
split_table AS (
  select
    method,
    map_keys(hist) as keys,
    map_values(hist) as vals
  from hist_table
)
select * from split_table
CROSS JOIN UNNEST(keys, vals) AS t (k, v);

result2

ちゃんちゃん。

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?