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

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.

Related

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

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

Amazon Redshift- How to get start date of the Week from existing daily date field from the table?

I am trying to get start date of the week from existing daily date field from the same table. For example daily dates from 05/08/2022 to 05/14/2022 , the start of the week date output need to come as 05/08/2022 for all days in the week. week start on Sunday.
Also similar thing require to first date of the Month and quarter(3 month division)
The date_trunc() function performs this operation - https://docs.aws.amazon.com/redshift/latest/dg/r_DATE_TRUNC.html

BigQuery partition on calendar week

Every week, I get a new dataset that I need to insert in BigQuery. The data can arrive on any day of the week. Once the data is ingested, I want to query data that arrived last week.
One option is to use date as partitioning when the data arrived but then the developers would need to know the exact date when data arrived to query the partition.
Instead of this, while ingestion, I want to create an INTEGER column which represents the calendar week of the year. The format will be 202005 or 202153 where former represents fifth week of 2020 and latter represents second last week of year 2021.
Since this is an integer, the only option for partition seems to be range partitioning. For it, BigQuery is asking for a start, end and interval. What values should I define?
I can define the following but as you can imagine that this sounds wrong
start 202001
end 203054
inerval 1
Update:
It seems that bigquery will only create partitions for which it has data. I checked that by executing
#legacySQL
SELECT
project_id, dataset_id, table_id, partition_id, TIMESTAMP(creation_time/1000) AS creation_time
FROM [PROJECT_ID:DATASET_ID.TABLE_ID$__PARTITIONS_SUMMARY__]
Another option would be to still Partition by date - but not ingestion date or whatever date you have in mind, rather start date of respective week with the help of DATE_TRUNC function
DATE_TRUNC(your_date, WEEK)
Note: You even can define start day of the week
WEEK(): Truncates date_expression to the preceding week boundary, where weeks begin on WEEKDAY. Valid values for WEEKDAY are SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

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

DATEDIFF - How many days until Sunday

In SQL Server, trying to write a age-off report for inventory purposes. Each week, the inventory system marks thousands of rows for deletion. This takes place on Sundays # 06:00:00 as part of weekly SQL DB purge schedule.
Using (yyyy-mm-dd hh:mm:ss:ms) format for closed_time, how can I calculate the numbers of days between that date, until next Sunday of the current week? And to be more elaborate, is there a way to narrow it down to the exact DD:HH:MM? The problem is the each client's Sunday DB schedule for purge varies. So that might be difficult to compute. Might be easier to just calculate whole days until Sunday 00:00:00. I tried using the DATEDIFF function with no success.
SELECT
Yada
DATEDIFF(DAY, closed_time,DW) AS Days_Until_Purged
FROM DB1
WHERE closed_time DESC
Thx in advance
If you choose any Sunday in the past (Such as 06:00 Sunday 2nd January 2000), you can calculate time that has GONE BY since then.
Then, if you take that and do modulo 7-days you get the time that has gone by since the most recent Sunday.
Then, if you do 7 - time_gone_by_since_last_sunday you get the time until the next sunday.
I'm going to do this in minutes to cope with a client that has a setting of 06:30.
DECLARE
#batch_processing_time SMALLDATETIME
SET
#batch_processing_time = '2000-01-02 06:00'
SELECT
(60*24*7) - DATEDIFF(minute, #batch_processing_time, closed_time) % (60*24*7)
FROM
yourTable
That's the number of minutes from each record's closed_time until the next #batch_processing_time.
Divide by (24*60) to get it in days.
try this:
select 8-DATEpart(w, closed_time) AS Days_Until_Purged from DB1 ...
This should solve your problem
SET DATEFIRST 1
Select DATEDIFF(dd,GETDATE(),DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),GETDATE()))