Dynamic month names as column aliases within Bigquery - sql

I am trying to create a table in which a few columns are flags to show whether an specific order was placed in one of the previous few months from the current date (dynamic). I would like the table aliases / names to show the month and year, all relative to the current date. Below is an example of my current code:
SELECT
order_id
,CAST(o.placed_at_local AS DATE) > DATE_TRUNC(CURRENT_DATE(), MONTH) AS in_current_month
,CAST(o.placed_at_local AS DATE) BETWEEN DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH)
AND DATE_TRUNC(CURRENT_DATE(), MONTH) AS in_previous_month
,CAST(o.placed_at_local AS DATE) BETWEEN DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 2 MONTH), MONTH)
AND DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH) AS in_previous_2_month
FROM my_table
I would like to change the column names so they are dynamic and showing the relevant month and year, e.g. "in_current_month" becomes "April_2022", "in_previous_month" becomes "March_2022", "in_previous_2_month" becomes "February_2022" and so on.
Anyone have an idea how to do this in Bigquery?
Thanks!

Related

How to retrieve first and last day of the previous month in Google BigQuery?

I am looking to create a get the first and last date of the previous month so I can do a WHERE clause with a between statement. It'll look something like this
WHERE
FirstSold_Date BETWEEN first_day_previous_month AND last_day_previous_month
Try this:
WHERE FirstSold_Date BETWEEN date_trunc(date_sub(current_date(), interval 1 month), month) AND last_day(date_sub(current_date(), interval 1 month), month)
I would not recommend between for this. Instead:
WHERE FirstSold_Date >= date_add(date_trunc(current_date, month), interval -1 month) and
FirstSold_Date < date_trunc(current_date, month)
The advantage of this approach is that the same logic works for timestamps and datetimes as well. Looking at the last date causes problems when times are involved.
Consider below
where date_trunc(FirstSold_Date, month) = date_trunc(date_sub(current_date, interval 1 month), month)

PostgreSQL last full n months

I have a query with dates for the last two years. I want to get data dynamically for the last 4 months.
That is if today 04/01/2021 I want to get data from 01/09/2020 up today inclusive.
The problem with my query is that I get data between 04/09/2020 up today, i.e. 4 months not including a full first month.
PostgreSQL
SELECT Category,
Product,
Sales,
Date
FROM Table
WHERE Date>= now() - INTERVAL '4 months'
What I need to change in my query ??
You can do it using date_trunc like following query.
SELECT Category,
Product,
Sales,
Date
FROM Table
WHERE Date>= date_trunc('month', current_date-interval '4 months')

Dynamic scheduled query based on timestamp

I am trying to set up a scheduled query to run on the 1st of each month, and capture one month of data. However it should not be the previous month, but 2 months previous - due to delays in data being loaded in to the source table. The source table is partitioned by day on session_timestamp so refining this as much as possible will be of benefit to reducing query cost.
So far I have this:
WHERE
EXTRACT(YEAR
FROM
session_timestamp) = EXTRACT(YEAR
FROM
DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH))
AND EXTRACT(MONTH
FROM
session_timestamp) = EXTRACT(MONTH
FROM
DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH))
This seems a highly inelegant solution but was intended to address cases where a year boundary would be crossed. However I can see from the "This script will process * when run." area that this is going to query everything in 2020 and not just in May 2020.
As you have pointed out, your query doesn't engage partition filter down to the 2 months of data which you want to query.
You don't have to do the year trick because DATE_TRUNC(..., MONTH) has year in it. Please try filter below:
-- Last day of the month
DATE(session_timestamp) <= DATE_SUB(DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH), INTERVAL 1 DAY)
AND
-- First day of the month
DATE(session_timestamp) >= DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 2 MONTH), MONTH)

Is there a way to add in column for Week number depending on a date column?

I was wondering if it is possible in SQL to read in a date column and based on that date create a new column and automatically have the Week number as well. For example today is 4/7/2020 , so the query would have Week 15 populated for that?
]1
In the picture the week column would ideally be populated beside 'datestr'.
Thank you]2
In redshift, you can use date_part() with the w specifier to get the week number of a date or timestamp:
select t.*, date_part(w, datestr) week_number from mytable t
Note that weeks starts on Monday in Redshift. If you want the week to start on Sunday:
select t.*, date_part(w, datestr + interval '1' day) week_number from mytable t
You could use extract. I am not 100% sure if weeks in Redshift start from Sunday or Monday, but you can adjust the interval to test the edge cases.
select datestr, extract(week from datestr + interval '1 day') as weeknum
from your_table

SQL to display data of current month & next month from table

I wanted some guidance on producing an SQL query that collects the table information of the current date and also next month without having to type in every day for the current month being October or the next month being November.
Basically I've got a table called WORK, in this table there are SHIFTID, DATEOFSHIFT, and MEMBERSHIPID. I basically need to list the SHIFTID's of shifts where MEMBERSHIPID = null and where DATEOFSHIFT is in November (next month)
Then I need to produce a query for the shift roster showing SHIFTID, DATEOFSHIFT, and MEMBERSHIPID of each shift in this current month.
This is the structure of my database table if needed.
I would recommend:
select w.*
from work w
where w.membershipid is null and
w.dateofshift >= trunc(sysdate, 'Month') + interval '1' month and
w.dateofshift < trunc(sysdate, 'Month') + interval '2' month;
You can also phrase the where as:
where w.membershipid is null and
trunc(w.dateofshift, 'Month') >= trunc(sysdate, 'Month') + interval '1' month
but this makes it hard for Oracle to use an index if an appropriate one is available.
Well from what you've provided, I infer that you want a query to display the information on all those fields for the current month. That is achievable by:
Select SHIFTID, DATEOFSHIFT, MEMBERSHIPID
From WORK
Where Month(DATEOFSHIFT)=MONTH(GETDATE());