Subtract 3 months to yyyymm month format column hive sql - date

In HiveSql I have a yearmonth [yyyymm] column from which I need to subtract 3 months
For example: if yearmonth is 201912 , the record required is 201909
Can someone please help me with the syntax or script I need to get for this ?
I have tried addmonths, conv(), and reg_extract
But nothing works

add_months() function works with dates. Convert yyyyMM to yyyy-MM-01 date, apply add_months and format as yyyyMM again:
with your_table as (select '201912' as yearmonth)
select date_format(add_months(concat_ws('-',substr(yearmonth,1,4),substr(yearmonth,5,2),'01'),-3),'yyyyMM') as yearmonth
from your_table;
Result:
201909

Related

How to extract Year and Month in SQL Postgres by using Data_Part function

I am facing an issue extracting the month and year from the data (in Character varying type) >> InvoiceDate in SQL Postgres. I have seen the solution is relatively easy with MySQL function: DATEFROMPARTS as per the below Code which is not available in SQLpostgres. How can I get the same result DATA_PART function in Postgres SQL, but simultaneously I need to change the data type of the column "InvoiceDate" to the date
Select
CustomerID,
min(InvoiceDate) first_purchase_date,
DATEFROMPARTS(year(min(InvoiceDate)), month(min(InvoiceDate)), 1) Cohort_Date
into #cohort
from #online_retail_main
group by CustomerID
The output:
Customer ID| first_purchase_date |Cohort_Date|
-----------+-------------------------+-----------+
12345 | 2010-12-20 15:47:00:00 | 2010-12-01|
I am trying to make a date consits of Year and Month , while the day to be set as 1 for all
Assuming a valid Postgres timestamp:
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
12/01/2010
--or ISO format
set datestyle = 'ISO,MDY';
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
2010-12-01
Uses date_trunc to truncate the timestamp to a month which means the first of the month. Then cast(::date) to a date type. The DateStyle just deals with how the value is presented to the user. The value is not stored formatted.
To do something similar to what you did in MySQL:
select make_date(extract(year from '2010-12-20 15:47:00.00'::timestamp)::integer, extract(month from '2010-12-20 15:47:00.00'::timestamp)::integer, 1);
This uses make_date from here Date/time functions and extract to build a date.

How to create 'Week column' based on another date column?

I have column 'jobstarttimeiso' and I want to create another column for Weeks of the year based on the date. How would I go about doing that? I am using Redash to query from redshift database. Please help! Thank you.
The extract function will return a week number e.g.
select extract(week from jobstarttimeiso) as weeknumber
In general:
EXTRACT ( datepart FROM { TIMESTAMP 'literal' | timestamp } )
See: https://docs.aws.amazon.com/redshift/latest/dg/r_EXTRACT_function.html
alternatively you can also use to_char()
TO_CHAR (timestamp_expression | numeric_expression , 'format')
with the parameter IYYY as the format for ISO 8601 week-numbering year (4 or more digits)

Subtract month from date column

I would like to subtract one month from a date column in Amazon Redshift (a fork of PostgreSQL 8.0.2).
So for each date column in a table, it will add another column date_minus_a_month.
I tried this code
Select date,date::date -interval '1 month'
from table
and received an error:
Interval values with month or year parts are not supported.
Does anyone have a solution for this?
You can use datesub, although I just use dateadd for everything and use negative numbers.
eg
SELECT getdate() as right_now, dateadd(month, -1, getdate()) as last_month
Docs: https://docs.aws.amazon.com/redshift/latest/dg/r_DATEADD_function.html

Produce a row for dates that do not exist in a table [duplicate]

I have a postgresql table userDistributions like this :
user_id, start_date, end_date, project_id, distribution
I need to write a query in which a given date range and user id the output should be the sum of all distributions for every day for that given user.
So the output should be like this for input : '2-2-2012' - '2-4-2012', some user id :
Date SUM(Distribution)
2-2-2012 12
2-3-2012 15
2-4-2012 34
A user has distribution in many projects, so I need to sum the distributions in all projects for each day and output that sum against that day.
My problem is what I should group by against ? If I had a field as date (instead of start_date and end_date), then I could just write something like
select date, SUM(distributions) from userDistributions group by date;
but in this case I am stumped as what to do. Thanks for the help.
Use generate_series to produce your dates, something like this:
select dt.d::date, sum(u.distributions)
from userdistributions u
join generate_series('2012-02-02'::date, '2012-02-04'::date, '1 day') as dt(d)
on dt.d::date between u.start_date and u.end_date
group by dt.d::date
Your date format is ambiguous so I guess while converting it to ISO 8601.
This is much like #mu's answer.
However, to cover days with no matches you should use LEFT JOIN:
SELECT d.d::date, sum(u.distributions) AS dist_sum
FROM generate_series('2012-02-02'::date, '2012-02-04'::date, '1 day') AS d(d)
LEFT JOIN userdistributions u ON d.d::date BETWEEN u.start_date AND u.end_date
GROUP BY 1

Select date only without the hour on postgresql [duplicate]

This question already has answers here:
Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL
(8 answers)
Closed 6 years ago.
I have date column on my postgres table as timestamp format i.e "2017-01-01 22:00:00". When I wrote in the queries
select date from table where date = '2017-01-01' it did not give me any result.
Should I always include the time information on those queries? Can I just put the yyyy-mm-dd only on my queries to search the date column?
regards
Cast to date:
select "date" from table where "date"::date = '2017-01-01'
Note that I enclosed references to the date column in double quotes, because date is a Postgres keyword. You should avoid naming your columns, tables, or schemas using keywords.
Use date() function, it will extract date from datetime like:
select DATE(my_field) from my_table;
Try this way:
select date("Date") from yourTable