让我对你说实话!
任何其他可行的解决方案都会比提出问题更好 - 原因很简单 - 这是错误的!它根本不返回丢失的数字!它而是在下一个间隙后显示数字。仅此而已(希望您会感谢我对此大开眼界)
现在,关于更好的解决方案 - 有很多选择供您追求。
注意:以下选项仅适用于 BigQuery!
选项1
BigQuery 标准 SQL - 请参阅如何启用标准 SQL
WITH YourTable AS (
SELECT 1 AS x UNION ALL
SELECT 2 AS x UNION ALL
SELECT 3 AS x UNION ALL
SELECT 6 AS x UNION ALL
SELECT 8 AS x UNION ALL
SELECT 10 AS x UNION ALL
SELECT 11 AS x
),
nums AS (
SELECT num
FROM UNNEST(GENERATE_ARRAY((SELECT MIN(x) FROM YourTable), (SELECT MAX(x) FROM YourTable))) AS num
)
SELECT num FROM nums
LEFT JOIN YourTable ON num = x
WHERE x IS NULL
ORDER BY num
选项 2
您可以在下面尝试BigQuery Legacy SQL(在这里您需要在 nums 表的选择表达式中设置开始/最小值和结束/最大值
SELECT num FROM (
SELECT num FROM (
SELECT ROW_NUMBER() OVER() AS num, *
FROM (FLATTEN((SELECT SPLIT(RPAD('', 11, '.'),'') AS h FROM (SELECT NULL)), h))
) WHERE num BETWEEN 1 AND 11
) AS nums
LEFT JOIN (
SELECT x FROM
(SELECT 1 AS x),
(SELECT 2 AS x),
(SELECT 3 AS x),
(SELECT 6 AS x),
(SELECT 8 AS x),
(SELECT 10 AS x),
(SELECT 11 AS x)
) AS YourTable
ON num = x
WHERE x IS NULL
选项 3
BigQuery Legacy SQL - 如果您不想依赖最小值和最大值并且需要设置这些值 - 您可以使用以下解决方案 - 它只需要设置足够高的最大值以适应您的预期增长(例如,我放了 1000)
SELECT num FROM (
SELECT num FROM (
SELECT ROW_NUMBER() OVER() AS num, *
FROM (FLATTEN((SELECT SPLIT(RPAD('', 1000, '.'),'') AS h FROM (SELECT NULL)), h))
) WHERE num BETWEEN 1 AND 1000
) AS nums
LEFT JOIN YourTable
ON num = x
WHERE x IS NULL
AND num BETWEEN (SELECT MIN(x) FROM YourTable) AND (SELECT MAX(x) FROM YourTable)
选项 4(出于某种原因 - 到目前为止我最喜欢的)
BigQuery 标准 SQL - 没有显式连接
WITH YourTable AS (
SELECT 1 AS x UNION ALL
SELECT 2 AS x UNION ALL
SELECT 3 AS x UNION ALL
SELECT 6 AS x UNION ALL
SELECT 8 AS x UNION ALL
SELECT 10 AS x UNION ALL
SELECT 11 AS x
)
SELECT num
FROM (SELECT x, LEAD(x) OVER(ORDER BY x) AS next_x FROM YourTable),
UNNEST(GENERATE_ARRAY(x + 1,next_x - 1)) AS num
WHERE next_x - x > 1
ORDER BY x