How to find only sunday from date column using bigquery - google-bigquery

I want to find out only Sunday present date column from range 1 NOV to 31 Dec 2021 using bigquery
Kindly suggest syntax for bigquery

You can create a function like the one below:
SELECT * FROM table WHERE date BETWEEN 11-01-2021 AND 12-31-2021 AND EXTRACT(DAYOFWEEK FROM date) = 1
Also, here you can learn more about how to create date functions into BigQuery.

Related

How to create a rolling period-over-period comparison in Redshift SQL

I have the following query that pulls all records from a Redshift table from January 1st of the current year through the final date of the most recent, full quarter.
SELECT *
FROM table
WHERE date_value BETWEEN DATE_TRUNC('year',getdate()) AND DATE_TRUNC('quarter',dateadd(day,-1,getdate()));
I now want to create a period-over-period comparison query that returns all records for the previous n months. Ex. if the first query returns all records for Jan - Jun 2022, this query will return all records for Jul - Dec 2021.
Here is what I have so far, however it currently returns Jan - Jun 2021 instead of the desired date range. I've tried playing around with DATEDIFF() instead of DATEADD() but haven't had any luck with that either. Any help is much appreciated.
SELECT *
FROM TABLE
WHERE date_value BETWEEN DATE_TRUNC('year',dateadd(year,-1,getdate())) AND DATE_TRUNC('quarter',dateadd(year,-1,getdate()));

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.

Get past 6 months data from a table based on the recent date present in the column

I have a database table which is named as table and there is a column Col which is present in table with datatype varchar. Column Col contains dates in the format MMM-YY.
For example Col has values as :
Col
DEC-21
NOV-21
SEP-20
OCT-19
DEC-21
As, we can see data can be duplicated like DEC-21. I want to extract last 6 months data based on the recent month present in Col. For example, If the DEC-21 is the most recent date(consider, day is not present) and so, I want data from DEC-21 to JUN-21 i.e. 12-21 to 06-21 if we map DEC to 12 and JUN to 06. This Table has many columns and one of the columns is Col which I mentioned above and I have to extract data based on the column Col by using SQL query.
I have written a query as:
SELECT *
FROM table
WHERE CAST(RIGHT(Col,4) AS INT) Between 2020 and 2021
But here I get data between 2020 and 2021. So, By doing some modification in the above query or Is there any other way to get the past 6 months data from the recent date which is in MMM-YYYY format from Col column.
I was writing code in R and I was using dbGetQuery() where I have to pass the SQL query. I have already done this thing after storing it in a dataframe but is there any way to do it directly by sql query ?
Any help would be appreciated.
with data as (
select *,
convert(date, '01-' + dt, 105) as converted_dt,
max(convert(date, '01-' + dt, 105)) over () as last_converted_dt
from T
)
select * from data
where converted_dt >= dateadd(month, -6, last_converted_dt);
The 105 comes from the list of date formats which can be found in the documentation for cast/convert. SQL Server can convert strings like Apr 2021 so a cast like below might also work (if you actually have four-digit years) but it's best to be explicit about the format as I did above.
cast(replace(dt, '-', ' ' as date)
Something like this should work.
SELECT * FROM table where CONVERT(DATE,'01-'+Col) BETWEEN '01-Jun-2021' and '31-Dec-2021'

Daily Partitioning and archiving

I'm trying to partition a table by month say Jan, Feb, Mar. The column through which I'll partition is a datetime type column with an ISO Format ('20190101', '20190201', etc).
For example, I have sales data for jan, feb, mar. I'd like the data to be partitioned by daily partitioned . ('20190101', '20190201','20190301' etc)
E.X:
Jan, Feb, Mar etc. Also I would like to keep the data less so I would like to delete daily day wise data keeping only 1 month data maximum, for example I will create jan 31 , feb 28 , mar 31, apr 30. How do I manage partition dynamically as some month is 31 days some are 28 days and 30 days. Also I need to retain only one month data for example if its, 1st of sep then I need to keep aug 31 days data, and can delete 31st jul day data now on 2nd sep I can delete 1st august data so I need to delete daily data and keep only 30 days data .
My question is: is it even possible? If it is, how an I automate the process using SSIS?
You may try this. As you want to remove previous 30 days data from current date or any specific date, so you can easily calculate the date range by subtracting 30 days from the current date.
For 30 days
--- Instead of getdate() you may use any of your date column for filter
delete from yourtable where yourdate < DATEADD( day, -30, getdate()) ---- here on place of 30 you may use any days you want to delete
For 1 month
--- Instead of getdate() you may use any of your date column for filter
delete from yourtable where yourdate < DATEADD( month, -1, getdate()) ---- here on place of 30 you may use any days you want to delete
Instead of having your partition boundaries be the end of the month, have them be the beginning. That is, do something like:
[2019-01-01, 2019-02-01),
[2019-02-01, 2019-03-01),
[2019-03-01, 2019-04-01),
[2019-04-01, 2019-05-01),
[2019-05-01, 2019-06-01),
etc
That is, the left-hand boundary is in the partition and the right-hand boundary isn't. If you're using actual Partitioning, you'd define your partition function as a "boundary left" function. See the documentation for more details.

How to retrieve the WeekofMonth for a given date in Hive

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