Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL (Snowflake) Create month table

I would like to create a monthly summary table to sum values where the date column is within two other different dates from another existing table.

I’ve tried with the following SQL code but Snowflake returns this error: SQL compilation error: error line 9 at position 10 invalid identifier 'DATE'.

select 
    dateadd(month, seq, dt::date) as DATE,
    year(DATE) as Y,
    month(DATE) as M,
    (select 
        sum(items.total_price_disc_conv)
    from BI.MODELS.CONTR_LINES_ACC as items
    where DATE between items.subscription_start_date and items.subscription_end_date
    ) as ARR
from (
    select 
        seq4() as seq,  
        dateadd(month, 1, '2019-12-01'::date) as dt 
    from table(generator(rowcount => (12 * 5))
    )
);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you are hitting this error because you try to use an ALIAS (DATE) in the WHERE filter. Can you try this one?

select 
    dateadd(month, seq, dt::date) as DATE,
    year(DATE) as Y,
    month(DATE) as M,
    (select 
        sum(items.total_price_disc_conv)
    from BI.MODELS.CONTR_LINES_ACC as items
    where dateadd(month, seq, dt::date) between items.subscription_start_date and items.subscription_end_date
    ) as ARR
from (
    select 
        seq4() as seq,  
        dateadd(month, 1, '2019-12-01'::date) as dt 
    from table(generator(rowcount => (12 * 5))
    )
);

Of course, it’s better to use MY_DATE instead of DATE.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading