Return the first day on previous month in Hive - hive

I try to return the count greater that first day of previous month from column 'dt' in HIVE table.
I use below, and don't work:
select count (*) from my_table
where dt > from_unixtime(unix_tmestamp(dt,'yyyyMMdd'))```

You can use below code to get first day of previous month based on dt.
add_months(trunc(dt,'MM'),-1)
Trunc will truncate date and make it a first day of the month of dt.
Add months will negate 1 month from first day of the month of dt.
Screenshot of output.

Related

Ruby Application Record: Select rows where Date's Month and Day is any of a list of Month Days

The following query selects rows where the date_field is a particular month and day:
Table.where("DATE_FORMAT(date_field, '%m/%d') = ?", "03/05")
I was wondering if there is a way to do something similar but instead, I supply a list of month/day strings ex: ["03/05","02,12", "12/25"] and get all rows where date_field matches that particular day and month.
You can use a SQL IN statement:
Table.where("DATE_FORMAT(date_field, '%m/%d') IN ?", ["03/05", "02/12", "12/25"])

Need help aggregating custom months into a snapshot field

I am working with a dataset that has a continues date field and I want to aggregate it at a monthly level where the month ends on the 15th day of the month. So each snapshot date would go from the 15th of the month to the 14th of the following month.
Example: Snapshot Date = 7/15/2021 would correspond with the date range of 6/15/2021 through 7/14/2021.
Is there an easy way to do this for all months in the table using SQL Server.
Just subtract 14 days and convert to a year/month format. One trick is to move everything to the last day of the month:
select eomonth(dateadd(day, -14, datecol)), count(*)
from t
group by eomonth(dateadd(day, -14, datecol));

HIVE query to return last date of previous month

How could I return the query for last day of previous month yyyy-mm-dd format in HIVE and also how to modify the code so that each month choose the last day?

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}

Data for specific date

My report gets data for the 1st of the current month. Let's say the 1st has still not come then how would I make the report show the data for the 1st of the previous month.
Thanks.
Simply use a select top 1 from your table, filtering by extract(day from yourDateColumn) = 1 to get only the rows with the data for the 1st day of any month, and order them in descending order by your date column (order by yourDateColumn desc), so that you always get the 1st day of the last available month in your table.
Docs for Oracle EXTRACT function