简体   繁体   中英

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

I have a table called toys which gives the total toys for a given date for a given name. I want to display what are the total toys for each day

Here is the schema:

create schema test_models 

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

The data in the table:

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');

This query returns the count of toys for a given date:

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')

Created a UDF to call in each row:

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) 
$$

This query is calling the 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

But I get this error when I run the above query with UDF, what am I doing wrong here. If

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

I am not sure why you have to use windowing function for this purpose, can below query get what you are after?

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 |
+-------------------------------+------------------+

As I understand, you generate dates, and you want to see some values for the total number of toys for all these dates even if there is no corresponding value in the toys table. Here is a sample SQL for this:

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 |
+-------------+-------+

As you see, even there is no data for 14-11, you still see the number of total toys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM