我正在使用SnowflakeDB并拥有下表
Key Seats
a 7,7
b 6,8
我想压扁它,同时在两个数字之间产生座位。我得到了扁平的部分,但无法弄清楚如何在两个提供的数字之间生成座位。
select a.*, b.val from mytable a, lateral flatten(input=> split(Seats,','))
上面的查询将生成下表。
Key Seats
a 7
a 7
b 6
b 8
b座位缺少七个。我希望最终的结果是
Key Seats
a 7
a 7
b 6
b 7
b 8
谢谢。
答案 0 :(得分:1)
最简单的方法是在展平前添加一个JavaScript函数,该函数创建一个输入密集序列的数组。
例如:
create or replace table mytable(key varchar, seats varchar) as
select * from values
('a', '7,7'),
('b', '6,9');
create or replace function fillme(STR varchar)
returns variant
language javascript
as '
let start = 1 * STR.split(",")[0];
let end = 1 * STR.split(",")[1];
let res = [];
for (let s = start; s <= end; s++) {
res.push(s);
}
return res;
'
;
/
select key, seats, fillme(seats) from mytable;
select a.*, b.value from mytable a, lateral flatten(input=> fillme(Seats)) b;
最后2个查询的输出是:
select key, seats, fillme(seats) from mytable;
-----+-------+---------------+
KEY | SEATS | FILLME(SEATS) |
-----+-------+---------------+
a | 7,7 | [ |
| | 7 |
| | ] |
b | 6,9 | [ |
| | 6, |
| | 7, |
| | 8, |
| | 9 |
| | ] |
-----+-------+---------------+
2 rows in result (first row: 421 msec; total: 423 msec)
select a.*, b.value from mytable a, lateral flatten(input=> fillme(Seats)) b;
-----+-------+-------+
KEY | SEATS | VALUE |
-----+-------+-------+
a | 7,7 | 7 |
b | 6,9 | 6 |
b | 6,9 | 7 |
b | 6,9 | 8 |
b | 6,9 | 9 |
-----+-------+-------+
假设你没有数百万的席位,这就足够了。