èªåããªãã¨ãªãBigQueryã®ã¯ã¨ãªãæ¸ãã¦ãã¦ããã£ãç½ ã«ã¤ãã¦åæãã¦ããã¾ãã ããã¥ã¡ã³ããã¡ããã¨èªãã°æ¸ãã¦ãã£ããããã®ã§ãããæ®æ®µã¯ããã¾ã§ç´°ããè¦ã¦ãªãã£ãããããã§ãããâ¦â¦ã
BigQueryのカレンダー | Advent Calendar 2023 - Qiita ã®16æ¥ç®ã®è¨äºã§ãã
CAST(value AS INT64) ã¯åãæ¨ã¦ã§ã¯ãªã
ä»ã®ããã°ã©ãã³ã°è¨èªãªã©ããã£ã¦ããã¨ãªãã¨ãªãæ´æ°åã«ãã£ã¹ãããã¨åãæ¨ã¦ã®ãããªæ°ããã¦ãã¾ãã¾ããBigQueryã¯éãã¾ãã åæ¨äºå ¥çãªæåã«ãªãã¾ãã
SELECT CAST(1.5 AS INT64) -- => 2
Returns the closest integer value. Halfway cases such as 1.5 or -0.5 round away from zero. https://cloud.google.com/bigquery/docs/reference/standard-sql/conversion_functions#cast_as_integer
DATE_DIFF(date1, date2, YEAR) ã®å¤ãå¢ããã®ã¯1å¹´å¾ã§ã¯ãªã
https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date_diff
1å¹´å¾ã§ã¯ãªãã¨ããã¨èªå¼ãããã¾ãããDATE_DIFFã§ã¯å·®ãè¨ç®ãããé¨åã®å·®ãåºãã¦ãã ãã£ã½ãã®ã§å¤§æ¦æ¥ã¨å æ¦ãæ¯ã¹ãã¨1æ¥ããçµã£ã¦ãªãã®ã«DATE_DIFF(date1, date2, YEAR) ã®å¤ã¯1ã«ãªãã¾ãã
SELECT DATE_DIFF('2024-01-01', '2023-12-31', YEAR); -- => 1
ã¡ããã¨365æ¥ãããçµéãããã©ãããå¤å®ããããã°æ¥æ°ãè¦ãã»ããããããã§ãã
åãWITHå¥ãä¸ã¤ã®ã¯ã¨ãªå ã§è¤æ°ååç §ãã¦ãããããéãçµæã«ãªã
BigQueryã§ã¯WITHå¥ã使ã£ã¦ãµãã¯ã¨ãªã«ååãä»ãã¦æ±ããã¨ãã§ãã¾ãã åãååã®WITHå¥ãåç §ããå ´ååãçµæã«ãªãã®ãæå¾ ããããªãã¾ãããããããåå¥ã«å®è¡ãããã®ã§ä¹±æ°ãã©ã³ãã ãªé åºã§SELECTãã¦LIMITããçµæãªã©ã¯ããããå¥ã ã«ãªãã¾ãã
WITH x AS ( SELECT RAND() AS r ) SELECT * FROM x UNION ALL SELECT * FROM x -- => åãWITHå¥ã®xãåç §ãã¦ããã®ã«1ã¤ç®ã®xã¨2ã¤ç®ã®xã§çµæãç°ãªã
対çã¨ãã¦ã¯WITHå¥ã§ã¯ãªãCREATE TEMP TABLEã§ä¸æ¦ãã¼ãã«ãä½ã£ã¦ãã¾ã£ã¦ãããåç §ããããå°ãããªããã¼ãªæ¹æ³ã ã¨WITH RECURSIVEã使ããã®ãããã¾ãã WITH RECURSIVE ã¯å帰çãªWITHå¥ãæ¸ãããã®è¨æ³ã§ãããããã使ãã¨WITHå¥ã®çµæããããªã¢ã©ã¤ãºãããã®ã§ä¸æ¦ãã¼ãã«ãä½ãã®ã¨åæ§ã®å¹æãããã¾ãã
WITH RECURSIVE x AS ( SELECT RAND() AS r UNION ALL SELECT * FROM x WHERE FALSE ) SELECT * FROM x UNION ALL SELECT * FROM x -- => WITH RECURSIVEã使ãã¨WITHå¥ã®çµæããããªã¢ã©ã¤ãºãããã®ã§çãããªã
NULL ã¨ã®æ¯è¼ã¯NULL(falsy)ã«ãªããç¹ã«NOT IN ã®æåããããã¥ãã
BigQueryã«éã£ã話ã§ã¯ãªãã§ããå¤ãNULLã®æã«!=ãªã©ã§ããã¨æ¯è¼ãããã¨çµæãNULLã«ãªã£ã¦ãã¾ãWHEREã®é¨åã§æå³ããfalsyã«ãªã£ã¦å°ããã¨ãããã¾ãã ç¹ã«value NOT IN (values) ã®æåã¯ãããã¥ããvaluesã«NULLãå«ã¾ãã¦ããå ´åãvalueãvaluesã«å«ã¾ãã¦ããã°FALSEãå«ã¾ãã¦ããªããã°NULLãè¿ããã¤ã¾ã常ã«TRUEã§ã¯ãªãfalsyãªçµæã«ãªã£ã¦WHEREãªã©ã«æ¸ããå ´åæ¡ä»¶ãæºãããã¨ã¯ããã¾ããã
SELECT 1 NOT IN (1, NULL, 2), -- => FALSE 2 NOT IN (1, NULL, 2), -- => FALSE 3 NOT IN (1, NULL, 2), -- => NULL
ä¸ã®ä¾ããããªãèããã°æ°ä»ããã¨æãã®ã§ãããvaluesã®é¨åããµãã¯ã¨ãªã«ãªã£ã¦ãããããã¨NULLãå«ã¾ãã¦ãããã¨ã¾ã§é ãåãããã£ããããã¡ã§ãã
, UNNEST([]) ããã¨ãã®è¡ãæ¶ãã
BigQueryã§ã¯é
åãå±éããããã®UNNEST(ARRAY)ã¨ããè¨æ³ãããã, UNNEST(ARRAY)
ã¨ããã¨é
åã®ããããã®è¦ç´ ãCROSS JOINã§ãã¾ãã
WITH x AS ( SELECT 1 AS n ) SELECT * FROM x, UNNEST([1, 2, 3]) AS i -- 以ä¸ã®çµæãå¾ããã -- 1, 1 -- 1, 2 -- 1, 3
ãã®æARRAYã空é åã ã¨ç©ºã®ãã¼ã¿ã¨JOINãããã¨ãããã¨ã«ãªã対å¿ããxã®ãã¼ã¿ãã¨æ¶ãã¦ãã¾ãã¾ãã ãã£ãããã®æåã失念ãã¦ããã¨ããã¯ãã®ãã¼ã¿ããªãç¶æ ã«ãªã£ã¦å°ãã¾ãã
WITH x AS ( SELECT 1 AS n ) SELECT * FROM x, UNNEST([]) AS i -- => 空é åãUNNESTãããã®ãCROSS JOINããã¨xã®æ¹ãã¨æ¶ãã
ãã®å ´åLEFT OUTER JOINãããã°xã®æ¹ã¯æ¶ããã«JOINããæ¹ã«ã¯NULLãå ¥ã£ã¦å¤§ä¸å¤«ã«ãªãã¾ãã
WITH x AS ( SELECT 1 AS n ) SELECT * FROM x LEFT OUTER JOIN UNNEST([]) AS i -- 以ä¸ã®çµæãå¾ããã -- 1, NULL
IN UNNEST(ARRAY) ãã§ãã
ããã¯ããã¾ãç½ ã¨ããããã§ã¯ãªãã§ããããµãã¯ã¨ãªããããã¨ã IN ã« UNNEST(ARRAY) ãæå®ããã°é åã«å«ã¾ãããã©ãããå¤å®ã§ãã¾ããæåã¯ãµãã¯ã¨ãªãæ¸ãå¿ è¦ãããã®ãã¨æã£ã¦ãã¾ããã
[NULL] ãä½ãã
BigQueryã§ã¯ã¯ã¨ãªã®æçµçµæã«NULLãå«ãé åãããã¨ã¨ã©ã¼ã«ãªãã¾ãã ãããä¸ã®NOT IN ã®ä¾ã«ãåºã¦ãã¾ãããã¯ã¨ãªã®éä¸çµæã§ã¯é åã«NULLãå«ã¾ãã¦ãã¦ãç¹ã«ã¨ã©ã¼ã«ã¯ãªãã¾ããã
SELECT [NULL] AS x -- Array cannot have a null element; error in writing field x ã¨ããã¨ã©ã¼ãåºã
ãã¼ãã«åã«.ãé£ç¶ãã¦ã¦ãåã
ããã¯å®å
¨ã«ããªãã¢ãªè©±ã§ãããBigQueryã®ãã¼ãã«åã®æå®ã§ã¯ããã¸ã§ã¯ãåããã¼ã¿ã»ããåã®ãã¨ã« .
ãä»ãã¾ããããã® .
ã¯ãªããè¤æ°ã¤ãã¦ãã¦ãã¨ã©ã¼ãªã©ã«ã¯ãªããæ®éã«åãããã§ããæååçã«ãã¼ãã«åãæ¤ç´¢ããããªããtypoã§.ãå¤ãã¦è¦ã¤ãããªãã¨ãããã¨ããããã¾ãã
SELECT * FROM `bigquery-public-data...........github_repos...................languages` LIMIT 1000