Skip to content

Instantly share code, notes, and snippets.

@terrierscript
Last active December 19, 2017 02:19
Show Gist options
  • Select an option

  • Save terrierscript/4366195fd7a87ca0b716a2e1d7aeaef6 to your computer and use it in GitHub Desktop.

Select an option

Save terrierscript/4366195fd7a87ca0b716a2e1d7aeaef6 to your computer and use it in GitHub Desktop.
Treasure Data & (一部Presto) の知っておいて便利だった関数 ref: https://qiita.com/inuscript/items/51b9b3c58260d823739c
SELECT
:
WHERE
-- 1日間のデータなら -1d。一ヶ月なら-30dとか。
TD_TIME_RANGE(time,
TD_TIME_ADD(TD_SCHEDULED_TIME(), '-1d', 'JST'),
null,
'JST'
)
SELECT *
FROM access
WHERE
url_extract_host(referer) = "google.com"
-- 正規表現でやるとこんな感じ
-- regexp_like(referer, '.*google.com/.*')
SELECT
TD_PARSE_AGENT(user_agent)['os_family']
TD_PARSE_AGENT(user_agent)['os_major']
TD_PARSE_AGENT(user_agent)['os_minor']
TD_PARSE_AGENT(user_agent)['ua_family']
TD_PARSE_AGENT(user_agent)['ua_major']
TD_PARSE_AGENT(user_agent)['ua_minor']
TD_PARSE_AGENT(user_agent)['device']
FROM access
WITH a AS (
SELECT
TD_PARSE_AGENT(user_agent)['ua_family'] AS family,
TD_PARSE_AGENT(user_agent)['ua_major'] AS version
FROM
access
),
b AS (
SELECT
if(family = 'IE', family || version, family) AS browser
FROM a
)
SELECT * FROM b
SELECT
*
TD_PARSE_AGENT(user_agent)['ua_family'] AS family,
TD_PARSE_AGENT(user_agent)['ua_major'] AS version
FROM
access
WHERE TD_PARSE_AGENT(user_agent) IN ['pc', 'smartphone']
SELECT TD_SESSIONIZE(time, 3600, ip_address) as session_id
FROM ...
ORDER BY ip_address, time
-- ↑ソートしないと正しく出ない!
SELECT
some_name,
map_keys(
histogram(some_type)
)
FROM foo
GROUP BY some_name
SELECT *
FROM access TABLESAMPLE BERNOULLI(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment