Generate Series Alternative for Snowflake 有效但生成的列表不完整
Generate Series Alternative for Snowflake works but generates an incomplete list
我正在考虑创建一个列表,我可以在其中查看在给定时间内生成了多少个 ID。因为我想独立于日期的小时提前显示从 8 到 21 的小时数,所以我使用了生成系列,现在需要找到 Snowfake 的等价物。这是我的查询:
series as (
SELECT seq4() as Hour
FROM TABLE(GENERATOR(rowcount => 21))
where Hour between 7 and 20
ORDER BY Hour),
ID_table as (
select extract(hour from date) as "Hour",
count(ID) as "Count"
from ID_table
group by 1)
select (Hour.Hour) + 1 AS "Hour",
id."Count",
from series as Hour
left join ID_table as id on id."Hour" = Hour.Hour
order by Hour.Hour;
出于某种原因,我只能看到 8 到 16 点,但是,我希望它显示 8-21 点,可能是什么问题?
您应该始终考虑到 SEQ() 函数不能保证间隙,因此为了生成范围,我建议您使用 ROW_NUMBER() 函数:
https://community.snowflake.com/s/article/Generate-gap-free-sequences-of-numbers-and-dates
无论如何,当我测试它时,我看到它 returns 预期数字:
SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21));
-- returnns numbers from 0 to 20
SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21)) where Hour between 7 and 20 order by hour;
-- returnns numbers from 7 to 20
with series as (SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21)) where Hour between 7 and 20 ORDER BY Hour)
select (Hour.Hour) + 1 AS "Hour"
from series as Hour;
-- returnns numbers from 8 to 21
会不会是browser/UI的东西?
我正在考虑创建一个列表,我可以在其中查看在给定时间内生成了多少个 ID。因为我想独立于日期的小时提前显示从 8 到 21 的小时数,所以我使用了生成系列,现在需要找到 Snowfake 的等价物。这是我的查询:
series as (
SELECT seq4() as Hour
FROM TABLE(GENERATOR(rowcount => 21))
where Hour between 7 and 20
ORDER BY Hour),
ID_table as (
select extract(hour from date) as "Hour",
count(ID) as "Count"
from ID_table
group by 1)
select (Hour.Hour) + 1 AS "Hour",
id."Count",
from series as Hour
left join ID_table as id on id."Hour" = Hour.Hour
order by Hour.Hour;
出于某种原因,我只能看到 8 到 16 点,但是,我希望它显示 8-21 点,可能是什么问题?
您应该始终考虑到 SEQ() 函数不能保证间隙,因此为了生成范围,我建议您使用 ROW_NUMBER() 函数:
https://community.snowflake.com/s/article/Generate-gap-free-sequences-of-numbers-and-dates
无论如何,当我测试它时,我看到它 returns 预期数字:
SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21));
-- returnns numbers from 0 to 20
SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21)) where Hour between 7 and 20 order by hour;
-- returnns numbers from 7 to 20
with series as (SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21)) where Hour between 7 and 20 ORDER BY Hour)
select (Hour.Hour) + 1 AS "Hour"
from series as Hour;
-- returnns numbers from 8 to 21
会不会是browser/UI的东西?