簡體   English   中英

SQL modeling question Window function [ROW_NUMBER() OVER (ORDER BY )] appears outside of SELECT, QUALIFY, and ORDER BY clauses

[英]SQL modeling question Window function [ROW_NUMBER() OVER (ORDER BY )] appears outside of SELECT, QUALIFY, and ORDER BY clauses

我有一張名為玩具的表格,它給出了給定名稱的給定日期的玩具總數。 我想顯示每天的玩具總數

這是架構:

create schema test_models 

create table toys 
(   
    name varchar(10),    
    number_toys int,    
    created_at timestamp 
);

表中數據:

insert into toys      
values ('M', 5, '2021-11-11T00:00:00.000Z'),  
       ('M', 10, '2021-11-12T00:00:00.000Z'),  
       ('M', 15, '2021-11-13T00:00:00.000Z'),  
       ('N', 4, '2021-11-11T00:00:00.000Z'),  
       ('N', 8, '2021-11-12T00:00:00.000Z'),  
       ('N', 12, '2021-11-13T00:00:00.000Z');

此查詢返回給定日期的玩具數量:

select sum(count) 
from 
    (select distinct 
         name, 
         last_value(number_toys) over (partition by name order by created_at asc) as count 
     from toys 
     where created_at <= '2021-11-12T00:00:00.000Z')

創建了一個 UDF 以在每一行中調用:

create or replace function count_toys
    (end_date_time VARCHAR(25)) 
returns number(30,0)  
as 
     $$    
     select sum(count) 
     from 
         (select distinct 
              name, 
              last_value(number_toys) over (partition by name 
                                            order by created_at asc) as count 
          from toys 
          where created_at <= end_date_time) 
$$

此查詢正在調用 UDF:

select
    row_number() over (order by null) as row_number,
    dateadd(day, row_number - 1,  '2021-11-11T00:00:00.000Z') start_date_time,
    dateadd(day, 1, start_date_time) as end_date_time,
    count_toys(end_date_time)
from 
    table (generator(rowcount => 3))
order by 
    start_date_time

但是當我使用 UDF 運行上述查詢時出現此錯誤,我在這里做錯了什么。 如果

SQL compilation error: Window function [ROW_NUMBER() OVER (ORDER BY null ASC NULLS LAST)] appears outside of SELECT, QUALIFY, and ORDER BY clauses

我不知道為什么你必須為此目的使用窗口 function,下面的查詢可以得到你想要的嗎?

SELECT created_at, sum(number_toys)
FROM toys
GROUP BY created_at;

+-------------------------------+------------------+
| CREATED_AT                    | SUM(NUMBER_TOYS) |
|-------------------------------+------------------|
| 2021-11-11 00:00:00.000000000 |                9 |
| 2021-11-12 00:00:00.000000000 |               18 |
| 2021-11-13 00:00:00.000000000 |               27 |
+-------------------------------+------------------+

據我了解,您生成日期,並且您希望查看所有這些日期的玩具總數的一些值,即使玩具表中沒有相應的值。 這是一個示例 SQL :

with timestamps as (       
select 
dateadd(day, row_number() over (order by null) - 1, '2021-11-11' )::DATE report_date
from table (generator(rowcount => 4)) 
),
totals as (
select created_at::date as created_date, sum(number_toys) total
from toys
group by created_date
)
select ts.report_date, IFNULL(t.total, lag(t.total) over (order by ts.report_date)) total
from timestamps ts 
left join totals t on ts.report_date = t.created_date
order by ts.report_date;

+-------------+-------+
| REPORT_DATE | TOTAL |
+-------------+-------+
| 2021-11-11  |     9 |
| 2021-11-12  |    18 |
| 2021-11-13  |    27 |
| 2021-11-14  |    27 |
+-------------+-------+

如您所見,即使沒有 14-11 的數據,您仍然可以看到玩具總數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM