[英]Snowflake table and generator functions does not give expected result
我嘗試創建一個簡單的 SQL 來跟蹤 query_history 的使用情況,但在使用table
和generator
函數(下面名為x
的 CTE)創建我的時隙時遇到了麻煩。
當使用我的時隙限制 query_history 時我沒有得到任何結果,所以過了一會兒我硬編碼了一個 SQL 以給出相同的結果(下面名為y
的 CTE)並且這工作正常。
為什么x
不起作用? 據我所知x
和y
產生相同的結果?
要測試示例,首先按原樣運行代碼,這不會產生任何結果。 然后將x as timeslots
並將y as timeslots
,這將給出所需的結果。
with
x as (
select
dateadd('min',seq4()*10,dateadd('min',-60,current_timestamp())) f,
dateadd('min',(seq4()+1)*10,dateadd('min',-60,current_timestamp())) t
from table(generator(rowcount => 6))
),
y as (
select
dateadd('min',n*10,dateadd('min',-60,current_timestamp())) f,
dateadd('min',(n+1)*10,dateadd('min',-60,current_timestamp())) t
from (select 0 n union all select 1 n union all select 2 union all select 3
union all select 4 union all select 5)
)
--select * from x;
--select * from y;
select distinct
user_name,
timeslots.f
from snowflake.account_usage.query_history,
x as timeslots
--y as timeslots
where start_time >= timeslots.f
and start_time < timeslots.t
order by timeslots.f desc;
(我知道代碼不是最優的,這只是為了說明問題)
序列號:
返回一個單調遞增的整數序列,帶有回繞。 環繞發生在 integer 寬度(1、2、4 或 8 字節)的最大可表示 integer 之后。
如果需要完全有序、無間隙的序列,請考慮使用 ROW_NUMBER window function。
為了:
with x as (
select
dateadd('min',seq4()*10,dateadd('min',-60,current_timestamp())) f,
dateadd('min',(seq4()+1)*10,dateadd('min',-60,current_timestamp())) t
from table(generator(rowcount => 6))
)
SELECT * FROM x;
應該:
with x as (
select
(ROW_NUMBER() OVER(ORDER BY seq4())) - 1 AS n,
dateadd('min',n*10,dateadd('min',-60,current_timestamp())) f,
dateadd('min',(n+1)*10,dateadd('min',-60,current_timestamp())) t
from table(generator(rowcount => 6))
)
SELECT * FROM x;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.