How to retrieve the WeekofMonth for a given date in Hive - sql

I have a date field in Hive 2018-06-10, from which i need to get WeekOfMonth
WEEKOFYEAR(order_time)
I need output for 2018-06-10 as 3 (which is 3rd week. assuming week starts from Sunday)
Is there any built in function in Hive to retrieve WeekofMonth. I couldn't find any. I tried below to convert based on minutes and seconds but
from_unixtime(unix_timestamp(CURRENT_DATE())+7200)
But the above is not giving correct value

For the week of the month, you can get the day part of the month and divide by 7.
select case
when DAYOFMONTH(order_time)%7 = 0
then DAYOFMONTH(order_time)/7
else DAYOFMONTH(order_time)/7 + 1
end

Also you can use date_format function:
select date_format('2018-06-10','W');
See more format patterns here: SimpleDateFormat

Related

Hive: Calculate exactly 1 year from date in format 'yyyy-MM-dd' string

I need to calculate if has passed exactly 1 year or more from this date '2021-01-29', in HIVE.
So the result date must be in 'yyyy-MM-dd' format, and equal to '2022-01-29' or later. '2022-01-28' it's not correct answer.
It's possible to use date_add('2021-01-29', interval 1 year), if so, could someone explain how?
Thank you in advance.
In newer versions of Hive since 1.2.0 you can add interval to the date:
select date('2021-01-29') + interval 1 year
Result:
2022-01-29
For old version of hive use this recipe:
1 Year = 12 months. Add 12 months using add_months function:
select add_months('2021-01-29',12)
Result:
2022-01-29
If you want to add more than one year, multiply 12 by the number of years.

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.

How to get the data for the last 12 months and split and month-wise in HIVE?

Table format for the date column is "yyyyMMdd" and I'm using the following functions to convert into standard format so that HIVE day, months and year can be performed to get the respective values.
(from_unixtime(unix_timestamp(cast(created_day as STRING) ,'yyyyMMdd'), 'yyyy-MM-dd'))
To get the current year data, I would subtract the year obtained from all the records with the year returned by the current date and if it return zero, then it falls in this year.
(year(current_date()) - year(from_unixtime(unix_timestamp(cast(created_day as STRING) ,'yyyyMMdd'), 'yyyy-MM-dd'))) = 0
Problem: If the current date falls in January, I would get only January data month, but i need to get the data from February(last year) to January(current year)?
Also I need to scale this to obtain the last 24 months.
I always set my date range parameters outside of Hive and pass them as arguments as this lends itself to reproducibility and testability.
select <fields> from <table> where created_day between ${hiveconf:start_day} and ${hiveconf:end_day}

Return last week data in hive

I am new to hive and sql.
Is there any way if we run a query today with count fields then it should fetch last 7 days data ( example- if i run a query with count fields on monday then I should get the total count from last week monday to sunday) And date in my table is in the format 20150910. (yyyyMMdd).
Kindly please help me on this.
You can use date_sub() in this case. Something like this should work...
select * from table
where date_field >= date_sub(current_date, 7)
assuming that the current day's data is not loaded yet. If you want to exclude the current day's data too, you will have to include that too in the filter condition
and date_field <= date_sub(current_date, 1)
current_date would work if your hive version > 0.12
else, you can explicitly pull the date from unix using to_date(from_unixtime(unix_timestamp()))

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