åå¼·ä¼ã§çºè¡¨ããã®ã§å 容ãé©å½ã«ã¾ã¨ãã¦ããã°è¨äºã«ãã¦ãã
ãã¤ã®éã«ããªãã¡ã¬ã³ã¹ãæ¥æ¬èªåããã¦ããã®ã§ãå
¨é¨ä¸èªããã人ã¯ãã¡ããèªãã ã»ãããããã
SQL Reference | BigQuery Documentation | Google Cloud Platform
ã¡ãªã¿ã«æ¥æ¬èªçã®ãªãã¡ã¬ã³ã¹ã ã¨æè¿ã®å¤æ´ç¹ãè¼ã£ã¦ããªãå ´åãããã®ã§ãææ°ã®æ
å ±ãç¥ãããå ´åã¯è±èªçãè¦ãæ¹ãããã§ã(URLã«?hl=en
ã足ãã¦ã¢ã¯ã»ã¹ããã°ãã(ï¼))
以ä¸ç®æ¬¡
- BigQueryã§ä½¿ãã2ã¤ã®SQL
- Standard SQLã使ãã¹ãï¼
- 便å©ãªæ©è½ãå¤æ´ç¹
- ãã¼ãã«åã®åç §
- COUNT(DISTINCT value)
- SELECT ãã¼ãã«å.*
- SELECT * EXCEPT (ã«ã©ã å)
- è¤æ°ã®REPEATEDåã®ã«ã©ã ãæã¤ãã¼ãã«ã®SELECT *
- SELECTã®ã«ã©ã åãªã©ã®å¾FROMã®åã®,ã®ç¦æ¢
- JOINãããã¼ãã«ã®ã«ã©ã ãSELECTããæã«åºåãããã«ã©ã åã®å¤æ´
- JOINã®æ¡ä»¶ã«ã ããããªãã§ãæ¸ããããã«ãªã£ã
- è¤æ°ã®ãã¼ãã«ãUNION ALLããã¨ãã®æ¸ãæ¹
- æ¥ä»ãã¨ã«åãããã¦ãããã¼ãã«ãæ¥ä»ã®ç¯å²ãæå®ãã¦åã£ã¦ãã
- WITH
- ãµãã¯ã¨ãª
- æ¥ä»ã®å
- ã¿ã¤ã ã¾ã¼ã³
- ãã£ã¹ã
- UDF
å人çã«ä¾¿å©ã«ãªã£ãã¨æãã®ã¯JOIN
ã®æ¡ä»¶å¶éã®ç·©åãWITH
ããµãã¯ã¨ãªã使ããå ´æã®å¶éã®ç·©åãã¿ã¤ã ã¾ã¼ã³ã使ããããã«ãªã£ããã¨ã§ã
BigQueryã§ä½¿ãã2ã¤ã®SQL
BigQueryã§ãã¼ãã«ã®ãã¼ã¿ãåå¾ããã¨ãã«ã¯ä»¥ä¸ã®2ã¤ã®SQLã使ãã¾ã
- Legacy SQL
- å ã BigQueryã§ä½¿ããSQLã§ãLegacyã¨ããååã§ããä»ã§ãããã©ã«ãã¯ããã§ã
- Standard SQL
- SQLæ¨æºã®ä»æ§ã«æ²¿ã£ã¦ãããããæ°ãã使ããããã«ãªã£ãSQLã§ããStandard SQLã使ãããã¨ãã¯ãªãã·ã§ã³ãæå®ããããWebã®ã³ã³ã½ã¼ã«ã®å ´åã¯1è¡ç®ã«
#standardSQL
çãªè¡ãæ¸ãã°ããã§ã
Standard SQLã使ãã¹ãï¼
Standard SQLã¯Legacy SQLã¨æ¯ã¹ãã¨ãããªäººã«åãã¦ãã¾ã
- ãµãã¯ã¨ãªãJOINãé§ä½¿ããè¤éãªã¯ã¨ãªãæ¸ã
- ä»ã®DBã§SQLã«è§¦ã£ããã¨ããããBigQueryã®Legacy SQLã«ã¯è§¦ã£ããã¨ããªã
- ããã¥ã¡ã³ããæ¯è¼ç詳細ãªæ¹ã使ããã(Legacy SQLã§ã¯LIKEãOFFSETã使ãã¾ããããã¥ã¡ã³ãã«ã¯èª¬æãè¦ã¤ãããã¾ããã§ãã)
- æ°ããæ©è½ã使ãã®ã好ã
ã¾ã以ä¸ã®Qiitaã®è¨äºã«ããã¨ãStandard SQLã®æ¹ãé«éã§ãæ°æ©è½ã追å ããã¦ãããã¨ãããã¨ãããã§ã
qiita.com
便å©ãªæ©è½ãå¤æ´ç¹
以ä¸é©å½ã«åæãã¦ããã¾ã
ã¹ã©ã¤ãããã³ãããã¦ããã®ã§ããã®ã«ãã£ã¦ã¯åããªããã(ï¼)
ãã¼ãã«åã®åç §
æ®éã®SQLã£ã½ããªãã¾ãã
[]
ã``
ã¯çç¥ã§ããã®ã§å²ã¾ãã«ããã¸ã§ã¯ãåãçç¥ããã°ä¸¡æ¹ã«å¯¾å¿ã§ãã¾ã
Legacy SQL | Standard SQL |
---|---|
[ããã¸ã§ã¯ãå:ãã¼ã¿ã»ããå.ãã¼ãã«å] |
`ããã¸ã§ã¯ãå.ãã¼ã¿ã»ããå.ãã¼ãã«å` |
COUNT(DISTINCT value)
Legacy SQL | Standard SQL |
---|---|
EXACT_COUNT_DISTINCT(value) |
COUNT(DISTINCT value |
Legacy SQLã®COUNT(DISTINCT value)
ã¯çµ±è¨çãªäºæ¸¬å¤ãè¿ãã®ã§ãæ£ç¢ºãªå¤ã欲ããã¨ãã¯EXACT_COUNT_DISTINCT(value)
ã使ããªãã¨ãããªãã¨ããç´ããããããã£ãã®ã§ããStandard SQLã§è§£æ¶ããã¾ãã
SELECT ãã¼ãã«å.*
JOIN
ããã¨ãã«SELECT ãã¼ãã«å.*
ã§ç¹å®ã®ãã¼ãã«åã®ã«ã©ã ã ãåã£ã¦ããã¾ã
SELECT * EXCEPT (ã«ã©ã å)
SELECT * EXCEPT (ã«ã©ã å)
ã§ç¹å®ã®ã«ã©ã ã ãé¤å¤ãã¦ãã®ä»ã®ã«ã©ã ãåå¾ã§ãã¾ã
è¤æ°ã®REPEATEDåã®ã«ã©ã ãæã¤ãã¼ãã«ã®SELECT *
ã¾ãSELECT *
ããããã¼ãã«ãREPEATEDå(ARRAYå)ã®ã«ã©ã ãè¤æ°æã£ã¦ããã¨Legacy SQLã§ã¯ã¨ã©ã¼ã«ãªã£ã¦ã¯ã¨ãªãå®è¡ã§ããªãã£ãã®ã§ãããStandard SQLã§ã¯å®è¡ã§ããããã«ãªãã¾ãã
SELECT
ã®ã«ã©ã åãªã©ã®å¾FROM
ã®åã®,
ã®ç¦æ¢
使ãã¥ãããªã£ãç¹ã¨ãã¦ã¯Standard SQLã§ã¯SELECT
ã®ã«ã©ã åãªã©ã®å¾FROM
ã®åã«,
ãããã¨ã¨ã©ã¼ã«ãªãããã«ãªã£ã¦ãã¾ãã¾ãã
JOIN
ãããã¼ãã«ã®ã«ã©ã ãSELECT
ããæã«åºåãããã«ã©ã åã®å¤æ´
ã¾ãSELECT ãã¼ãã«å.date
ã®ãããªã«ã©ã ãSELECT
ããå ´åãLegacy SQLã§ã¯çµæã¨ãã¦ãã¼ãã«å_date
ã¨ããã«ã©ã åã«è§£éããã¦ããã®ã§ãããStandard SQLã§ã¯date
ã¨è§£éããã¦ãã¾ãããã«ãªã£ãã®ã§ãååã®ã«ã©ã ãè¤æ°SELECT
ããå ´åã«ã¯æ示çã«å¥ã®ã¨ã¤ãªã¢ã¹ãæå®ããªãã¨ãããªããªãã¾ãã
JOIN
ã®æ¡ä»¶ã«ã ããããªãã§ãæ¸ããããã«ãªã£ã
Legacy SQLã ã¨ä»¥ä¸ã®ããã«ã«ã©ã åã®ä¸è´ã®æ¡ä»¶ããæ¸ããªãã£ã
ON ãã¼ãã«ã®å¥å1.id = ãã¼ãã«ã®å¥å2.id
Standard SQLã ã¨ã ããããªãã§ãæ¸ããããã«ãªã£ã¦ãã¾ã
ON ãã¼ãã«ã®å¥å1.id = ãã¼ãã«ã®å¥å2.id
ON ãã¼ãã«å1.id = ãã¼ãã«å2.id
USING (id)
ON ãã¼ãã«ã®å¥å1.id < ãã¼ãã«ã®å¥å2.id
ON ãã¼ãã«å1.id + 100 = ãã¼ãã«å2.id
è¤æ°ã®ãã¼ãã«ãUNION ALL
ããã¨ãã®æ¸ãæ¹
Legacy SQLã ã¨ãã¼ãã«åã,
ã§ã¤ãªããã ãã§æ¸ãã¾ã
FROM ãã¼ãã«1, ãã¼ãã«2
Standard SQLã ã¨ã¡ããã¨UNION ALL
ãæ¸ããªãã¨ãããªããªã£ã¦ãã¾ãã¾ãã
FROM (SELECT * FROM ãã¼ãã«1 UNION ALL SELECT * FROM ãã¼ãã«2)
æ¥ä»ãã¨ã«åãããã¦ãããã¼ãã«ãæ¥ä»ã®ç¯å²ãæå®ãã¦åã£ã¦ãã
BigQueryã§ã¯ãã¼ãã«ãæ¥ä»ãã¨ã«åããã®ãããè¡ããã¦ããããã¼ãã«åã¯ããã¼ãã«åã®å
é é¨å+æ¥ä»ãã®å½¢å¼ã«ããã®ãå¤ã
Legacy SQLã ã¨TABLE_DATE_RANGE
é¢æ°ãªã©ã使ã
FROM TABLE_DATE_RANGE([ãã¼ãã«åã®å
é é¨å], TIMESTAMP('2017-06-23'),TIMESTAMP('2017-06-24'))
Standard SQLã ã¨*
ã§æå®ããé¨åã_TABLE_SUFFIX
ã«å
¥ãã®ã§WHERE
ã§æ¡ä»¶ãæå®ã§ãã
FROM `ãã¼ãã«åã®ãã¬ãã£ã¯ã¹*` WHERE _TABLE_SUFFIX BETWEEN '20170623' AND '20170624'
WITH
Standard SQLã§ã¯WITH
ã使ã£ã¦ãµãã¯ã¨ãªã«ååãä»ãã¦ä½¿ãã¾ãããã¨ãã§ãã¾ã
ããã使ããã¨ã§è¤éãªã¯ã¨ãªã®å ´åã«ãä½å±¤ããµãã¯ã¨ãªã使ã£ã¦ãå¤å°ãããããããªã£ãããå
±éé¨åãWITH
ã§ã¾ã¨ãããã§ãã¾ã
ã¡ãªã¿ã«BigQueryã®WITH
ã§ã¯å¼æ°ãä¸ãããå帰ã¯ã§ããªãã§ã
âãã¾ãæå³ã®ãªãWITH
ã使ã£ãã¯ã¨ãªã®ä¾
#standardSQL WITH data AS (SELECT 1 AS x, 2 AS y UNION ALL SELECT 3 AS x, 4 AS y) SELECT SUM(x) AS sum_x, SUM(y) AS sum_y FROM data
ãµãã¯ã¨ãª
Legacy SQLã§ã¯FROM
ã®ä¸ã«ãããµãã¯ã¨ãªãæ¸ãã¾ããã§ããããStandard SQLã§ã¯ãããããªã¨ããã«æ¸ããããã«ãªãã¾ãã
ãã®ãããã§ORDER BY RAND()
ã¿ãããªãã¨ãã§ããããã«ãªã£ã¦ã¾ã
æ´ã«ãµãã¯ã¨ãªãå¤å´ã®å¤ã«ä¾åãã¦ããç¸é¢ãµãã¯ã¨ãªãæ¸ããããã«ãªãã¾ãã
æ¥ä»ã®å
Legacy SQLã§ã¯TIMESTAMP
åãããªãã£ãã®ã§ãããStandard SQLã§ã¯TIME
åãDATETIME
åãDATE
åãå ããã¾ãã
TIMESTAMP
åã¯UTCã®æ¨æºæã®æ¥æã表ãã¦ãã¦ããã®ä»ã¯ãã¼ã«ã«ã®æ¥æã表ãã¦ãã¾ã
ã¿ã¤ã ã¾ã¼ã³
Standard SQLã§ã¯ã¿ã¤ã ã¾ã¼ã³ã使ããããã«ãªã£ãã®ã§ä»¥ä¸ã®ããã«æ¸ããããã«ãªãã¾ãã(Legacy SQLã ã¨æ¥æ¬æéã¨ã®éã®å¤æã§9æéããããã¨ãèããªãã¨ãããªãã£ã)
TIMESTAMP('2017-06-24 00:00:00', 'Asia/Tokyo')
TIMESTAMP('2017-06-24 00:00:00+09')
æ¥æãªã©ãåãåºãã¨ãã以ä¸ã®ããã«ã¿ã¤ã ã¾ã¼ã³ãæå®ããã°9æéããããã¨ããã¾ãèããã«æ¥æãæ±ãã¾ã
EXTRACT(MONTH FROM timestamp AT TIME ZONE 'Asia/Tokyo')
DATE(timestamp, 'Asia/Tokyo')
STRING(timestamp, 'Asia/Tokyo')
ãã£ã¹ã
Legacy SQLã§ã¯ä»¥ä¸ã®ãããªæãã§ãã
FROAT('2.0')
INTEGER(1.5)
Standard SQLã§ã¯CASTé¢æ°ãªã©ã使ãã¾ã
CAST('2.0' AS FLOAT64)
CAST(1.5 AS INT64)
ã¡ãªã¿ã«Standard SQLã®æµ®åå°æ°ã®ãã£ã¹ãCAST(1.5 AS INT64)
ã¯åãæ¨ã¦ã§ã¯ãªãåæ¨äºå
¥ãããã®ã§æ³¨æ(-1.5ã®å ´åã¯-2)
UDF
ãã®ããã°ã§ä½åã触ãã¦ãã¾ãããJavaScriptãSQLã§é¢æ°ãå®ç¾©ãã¦å¼ã¹ãã®ã§ä¾¿å©ã§ã
Legacy SQLã§ãStandard SQLã§ã使ãã¾ãããStandard SQLã«ãªã£ã¦ã¡ãã£ã¨ä½¿ãããããªãã¾ãã
BigQueryで配列に添字をつける(複数の配列をzipする) - 唯物是真 @Scaled_Wurm
BigQueryで単語分割がしたい - 唯物是真 @Scaled_Wurm
BigQueryのWebコンソールでJSONを整形して出力(pretty print) - 唯物是真 @Scaled_Wurm