GENERATE_ARRAY()関数ã使ãã¨çå·®æ°åãä½ããã®ã§å¾ã¯JOINãã¦ããã°ãã ã
åã«ä»¥ä¸ã®è¨äºã§ã¨ã©ãã¹ããã¹ã®ãµãããªã©ã調ã¹ãããSQLã§ã¯è©¦ãå²ãã«ããæ¹æ³ãããããæ¸ãããã«ãªãã£ã
sucrose.hatenablog.com
試ãå²ã
ããæ°ããã®æ°ãããå°ããª1以å¤ã®æ°ã§å®éå²ã£ã¦ã¿ã¦ãå²ãåããªããã°ç´ æ°ã
ããããã¹ã¦ã®æ°ã«ã¤ãã¦ç¢ºããã
以ä¸ã®ã¯ã¨ãªãåããã¦ã¿ãããå¤å®ãããæ大ã®æ°ã\(N\)ã¨ããæ\(N\)ãåã«ãããå®è¡æéã¯4åãããå¢ãã¦ãã(\(O(N^2)\)ã£ã½ãé°å²æ°)
ãªã®ã§æ大ã§ãæ°ä¸ãããã¾ã§ããè¨ç®ã§ããªãã£ã
#standardSQL #æ°åãä½ã WITH numbers AS ( SELECT i FROM UNNEST(GENERATE_ARRAY(3, 20000, 2)) AS i ) #å²ãåããã試ã , divisible AS ( SELECT a.i , MOD(a.i, b.i) = 0 AS is_divisible FROM numbers AS a JOIN numbers AS b ON SQRT(a.i) + 1 > b.i ) #1åãå²ãåãã¦ããªããã®ãç´ æ° SELECT * FROM UNNEST([2, 3]) AS i UNION ALL SELECT i FROM divisible GROUP BY i HAVING NOT LOGICAL_OR(is_divisible) ORDER BY i