在Redshift中获取缺少id的行

时间:2016-12-15 13:51:29

标签: sql amazon-web-services amazon-redshift

我有类似

的东西
id | name
---|-----
1  | Sarah
3  | Pat
4  | Lea

我正在寻找丢失的行。我尝试使用generate_series和左连接,但这是您在Redshift中无法做到的事情,因为不支持generate_series

没有临时表可以做到吗?

修改

最后做了with a temporary table(0到1_000_000)看到答案。

1 个答案:

答案 0 :(得分:0)

这可能不是最佳的。但这就是我的做法

-- create temporary table
CREATE TABLE series (id INT) SORTKEY(id);

-- insert 0 to 1_000_000
INSERT INTO series WITH seq_0_9 AS
(SELECT 0 AS num
UNION ALL SELECT 1 AS num
UNION ALL SELECT 2 AS num
UNION ALL SELECT 3 AS num
UNION ALL SELECT 4 AS num
UNION ALL SELECT 5 AS num
UNION ALL SELECT 6 AS num
UNION ALL SELECT 7 AS num
UNION ALL SELECT 8 AS num
UNION ALL SELECT 9 AS num),
                    seq_0_999 AS
(SELECT a.num + b.num * 10 + c.num * 100 AS num
FROM seq_0_9 a,
    seq_0_9 b,
    seq_0_9 c)
SELECT a.num + b.num * 1000 AS num
FROM seq_0_999 a,
 seq_0_999 b
ORDER BY num;

-- Why not
VACUUM series;

-- LEFT OUTER JOIN with table inverted and with the interval
SELECT *
FROM series
LEFT OUTER JOIN other_table ON series.id = other_table.id
WHERE series.id BETWEEN 0 AND 4
ORDER BY series.id;