Created
May 13, 2016 11:42
-
-
Save terrierscript/1fe7b5d589c361f0a5f9322689dd1eb5 to your computer and use it in GitHub Desktop.
MySQLしかろくに使ってこなかった人間が知って便利だった Presto & Treasure Data 小ネタ ref: http://qiita.com/inuscript/items/061a6ab5fd6564e8686c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT * | |
| FROM access | |
| WHERE | |
| url_extract_host(referer) = "google.com" | |
| -- 正規表現でやるとこんな感じ | |
| -- regexp_like(referer, '.*google.com/.*') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT * FROM ( | |
| SELECT * FROM ( | |
| SELECT * AS day | |
| : | |
| ) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| EXPLAIN (FORMAT GRAPHVIZ) | |
| SELECT * FROM ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT * | |
| FROM access TABLESAMPLE BERNOULLI(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| some_name, | |
| map_keys( | |
| histogram(some_type) | |
| ) | |
| FROM foo | |
| GROUP BY some_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| : | |
| WHERE | |
| -- 1日間のデータなら -1d。一ヶ月なら-30dとか。 | |
| TD_TIME_RANGE(time, | |
| TD_TIME_ADD(TD_SCHEDULED_TIME(), '-1d', 'JST'), | |
| null | |
| 'JST' | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 b | |
| ), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT TD_SESSIONIZE(time, 3600, ip_address) as session_id | |
| FROM ... | |
| ORDER BY ip_address, time -- ソートする |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH a AS ( | |
| SELECT * FROM some_table | |
| ), | |
| b AS ( | |
| SELECT * FROM a | |
| ), | |
| c AS ( | |
| SELECT * FROM b | |
| ) | |
| SELECT * FROM c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH a AS ( | |
| SELECT * FROM some_table | |
| ), | |
| b AS ( | |
| SELECT * FROM a | |
| ), | |
| c AS ( | |
| SELECT * FROM a | |
| ) | |
| SELECT * FROM c, b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH | |
| config AS ( | |
| SELECT 2 AS val_a | |
| ), | |
| tbl AS ( | |
| SELECT | |
| val_a, | |
| some_value * val_a -- some_value * 2 | |
| FROM some_table, config | |
| ) | |
| SELECT * FROM tbl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH config AS ( | |
| SELECT | |
| TD_TIME_ADD(TD_SCHEDULED_TIME(),'-10d', 'JST') AS start_time, | |
| TD_TIME_ADD(TD_SCHEDULED_TIME(),'-1d', 'JST') AS end_time | |
| ) | |
| SELECT * | |
| FROM access, config | |
| WHERE TD_TIME_RANGE(time, config.start_time, config.end_time, 'JST') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT a || '_' || b | |
| FROM * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- IEだけversion付けたいみたいなとき | |
| SELECT | |
| if(family='IE', family || version, family) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT coalesce(a, b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| CASE ua | |
| WHEN 'IE' THEN family || '_'|| version | |
| WHEN 'Chrome' THEN family | |
| WHEN 'Firefox' THEN family | |
| WHEN 'Safari' THEN family | |
| WHEN 'Opera' THEN family | |
| ELSE 'Other' | |
| END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment