简体   繁体   中英

Snowflake retrieving data of past month on a particular date of current month

I am new to snowflake and my manager wants me to retrieve the data of the past month when it is 5th of the current month. For example if today is 5th April, then ask snowflake to retrieve the data of the past month ie from 1st March 2021 to 31st March 2021 and similar for all the other months.

The reason why he wants to update the last month data on 5th of every next month because that is the day when we get the data.

I tried to use the DATEADD function but it is more complicated than just using this function.

Thanks in advance!

PS: The data for every month has same date. for example: the date is like - April 20th will be stored in the database as "2021-4-01" - and same for April 25th date will be stored as "2021-4-01".

The day doesn't change in the database, just the month and year.

as to the prior month window that can be done via DATE_TRUNC and DATEADD

select 
    current_date as cd
    ,date_trunc('month', cd) as end_range
    ,dateadd('month', -1, end_range) as start_range
    ;

gives:

CD          END_RANGE   START_RANGE
2021-04-21  2021-04-01  2021-03-01

the other half of the question only do it on the 5th, if you have a task run daily etc. can be solved via

,day(current_date) = 5 as is_the_fifth

or if in an inline way

iff(day(current_date) = 5, <do true stuff>, <do false stuff>)

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