How to add/subtract weeks to the third day of the week? - sql

I'm trying to figure out how to translate this line of PROC SQL code into Snowflake SQL but haven't found a way yet.
%LET last_post_dt = %SYSFUNC(INTNX(WEEK.3,%SYSFUNC(TODAY(),),-2,B),DATE9.);
Basically, its subtracting 2 weeks from the third day of the current week (monday = 1).
I've tried altering the session by using WEEK_START and other functions such as DATEADD, but haven't been able to solve this.
Thanks in advance!

You can use date_trun() to get whatever today is to a Monday. Then add 2 for 3rd day of the week, and then subtract 2 weeks from that.
SELECT DATE_TRUNC('week',current_date()) as week_start,
DATEADD('day',2,week_start) as week_3rd_day,
DATEADD('week',-2,week_3rd_day) as weeks_ago,
DATEADD('week',-2,DATEADD('day',2,DATE_TRUNC('week',current_date()))) as all_in_one_line_date

Related

How can I get the same week day from previous year in Tableau using a calculated field? For example the week day of 06/02/2020

I need to add the week day from previous year on the 3rd column.
Form example 06/02/2021 was on a Wednesday. I need the week day of 06/02/2021.
Thanks
try this:
SELECT DATENAME(weekday,DATEADD(YEAR, -1, '06-02-2021'))
Tableau is notoriously difficult to figure out work days. As some have suggested, it may be easier to use the underlying database to calculate this. However, if you need to do this in Tableau it can be accomplished like this:
DATEADD(
"day"
,CASE LEFT(LOWER(DATENAME("weekday",[DateFieldUsed])), 3)
WHEN 'fri' THEN 3
WHEN 'sat' THEN 2
ELSE 1
END
,[DateFieldUsed]
)
Basically, if it is Friday you need to add 3 days to the date, Saturday you need to add, and in any other situations just add 1 date. (please test, not doing this in Tableau so the numbers may be off).
I may have misunderstood the question, the example isn't exactly what you're asking. If your current value is 6/2/2021, and you are trying to find the week day of 6/2/2020, I would do this:
DATENAME("weekday", DATEADD(year, -1, [DateFieldUsed])

Hive - Query to get Saturday as week start date for a given date

I have an requirement in hive to calculate Saturday as week start date for a given date in hive sql.
Eg)
Date week_start
03-27-2021 03-27-2021
03-28-2021 03-27-2021
03-31-2021 03-27-2021
04-07-2021 O4-03-2021
04-09-2021. 04-03-2021
I tried using pmod and other date functions but not getting desired output. Any insight is much appreciated.
Hive offers next_day(), which can be adapted for this purpose. I think the logic you want is:
select date_add(next_day(date, 'SAT'), -7)
This is a little arcane. next_day() gets the next date after the argument date with a given day of the week. So, go to the next Saturday and then subtract 7 days for the start of the week.

Best way to break down by weeks in BigQuery

So what I'm looking to do is create a report that shows how many sales a company had on a weekly basis.
So we have a time field called created that looks like this:
2016-04-06 20:58:06 UTC
This field represents when the sale takes place.
Now lets say I wanted to create a report that gives you how many sales you had on a weekly basis. So the above example will fall into something like Week of 2016-04-03 (it doesn't have to exactly say that, I'm just going for the simplest way to do this)
Anyone have any advice? I imagine it involves using the UTEC_TO_xxxxxx functions.
The documentation advises to use standard SQL functions, like DATE_TRUNC():
SELECT DATE_TRUNC(DATE '2019-12-25', WEEK) as week;
you can use WEEK() function - it gives you week number
SELECT WEEK('2016-04-06 20:58:06 UTC')
if you need first day of the week - you can try something like
STRFTIME_UTC_USEC((UTC_USEC_TO_WEEK(TIMESTAMP_TO_USEC(TIMESTAMP('2016-05-02 20:58:06 UTC')), 0)),'%Y-%m-%d')
I had to add parentheses:
SELECT DATE_TRUNC(DATE('2016-04-06 20:58:06 UTC'), WEEK) as week;
This is quite an old question and things have moved on since.
In my case, I found that the old WEEK function is no longer recognised, so I had to instead use the EXTRACT function. The doc for it can be found here.
For me it was enough to just extract the ISOWEEK from the timestamp, which results in the week of the year (the ISOYEAR) as a number.
ISOWEEK: Returns the ISO 8601 week number of the datetime_expression. ISOWEEKs begin on Monday. Return values are in the range [1, 53]. The first ISOWEEK of each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year.
So I did this:
SELECT EXTRACT(ISOWEEK FROM created) as week
And if you want to see the week's last day, rather than the week's number in a year, then:
SELECT last_day(datetime(created), isoweek) as week

Teradata Change format of Week Number

I'm pretty new to SQL so I hope this isn't a dumb question, tried to google but couldn't find anything.
I'm summing sales of departments per week in SQL and am using TD_SYSFNLIB.WEEKNUMBER_OF_YEAR (trans_dt) to get the week number.
I think everything is working except I'd like to change the format of the weeks to the start date of the week, e.g. week 1 = 1/4/15
Also, i'm not sure how to handle the very first of the year week 0 since I think that should be grouped up with week 52 of last year.
The following date math trick should get you Beginning of Week as an actual date without having to join to the SYS_CALENDAR view or using a function:
SELECT CURRENT_DATE - ((CURRENT_DATE - DATE '0001-01-07) MOD 7) AS BOW;
Starting with TD14 there's NEXT_DAY which returns the following weekday, if you subtract 7 days you get the previous day:
next_day(trans_dt - 7, 'sunday')

create week function, but instead of Sunday start day of the week, Monday

Anyone know of a script, that creates a week table, that is based on Monday as the start day of the week and not Sunday? For MS SQL
You can adjust the way SQL Server sets the first day of the week using http://msdn.microsoft.com/en-us/library/ms181598.aspx if that helps?
Basically this is putting every sunday to the week before, with week=0->week=52