Generate date array with all dates in between week range - sql

I am trying to generate a date array for joining purposes, where I have all dates relative to a week range in the second column.
i.e.
SELECT
generate_date_array(date(2021,1,1),date(2021,11,26),INTERVAL 6 DAY)
Produces
What I need is a second column, that shows all the dates between each date row.
i.e.
Since I am ultimately joining this to another table, to see what week a record creation date is in, for this interval, I'm wanting to make this a table with two columns, and a row for each entry.

There are many way of doing this - below is just one and to re-use the "code" you already started with
select week, week + step as date
from (
select
generate_date_array(date(2021,1,1),date(2021,11,26), interval 6 DAY) weeks
), unnest (weeks) week, unnest([0, 1, 2, 3, 4, 5]) step
with output

Related

SQL week number for the whole table

How to create a new column which calculates week number but for the whole table ignoring year?
Desired output is as follows:
Appreciate any help :)
You can do this by calculating 1st day of week of oldest row, and then calculate day diff of 1st day of week of current row and coldest row, after that, divide it by 7 days plus 1 will give you the desired week number across the full table.
Assuming you are using MySQL and the first day of the week is Sunday:
WITH min_week_start AS (
SELECT
SUBDATE(MIN(record_date), dayofweek(MIN(record_date)) - 1) as week_start_date
FROM
record_table
),
record_week_start AS (
SELECT
record_date,
SUBDATE(record_date, dayofweek(record_date) - 1) as week_start_date
FROM
record_table
)
SELECT
record_week_start.record_date,
DATEDIFF(record_week_start.week_start_date, min_week_start.week_start_date) / 7 + 1 as week_num
FROM
record_week_start
CROSS JOIN
min_week_start

SQL filter by two date columns in the same month

In my dataset I have column A and column B, they have different dates, sometimes one column has a date and the other don't, sometimes both have values but different months. I want to filter by the date when they are in the January.
I want both columns to have date of January '2023-01-01'
I started with
SELECT *
FROM table
WHERE DATE_TRUNC(SAFE_CAST(column_a AS DATE), MONTH) = '2023-01-01'
OR DATE_TRUNC(column_b, MONTH) = '2023-01-01'
The problem is that in some cases the column_a has a date prior January due to the or. And both dates need to be in the some month.
How can I solve that?

Create n columns on bigquery

I want to generate a new table in bigquery using months between two dates (let's say from 2016-01-01 until 2019-05-01)
I know how to generate an array of months, such as
SELECT
GENERATE_DATE_ARRAY('2016-01-01', '2019-05-01', INTERVAL 1 MONTH) AS months
How do I create columns for each of the month generated in the array above?
I want to create a counter month by month, having months as columns

DAX Counting Values in previous period(s)

I have a Month Column with the Month Field populated for each line for the 100K of lines of data I have.
I need to count the amount of times the Month Field is populated in the Previous Month (Period).
I also need to count the total amount of times the Month Field is populated in the Previous 11 months as well.
This is a rolling count for each months reporting that I do..
table name: 'ws pds' and field name [Month Tagged]
You can utilize the powerful time intelligence functions in DAX such as PARRALLELPERIOD to look at values from previous months. But in order to make use of these functions you need to create a calendar/date entity. Mark that entity as a Date table. And join to it by date from your "ws pds" table. The Date dimension should span the timeframe of your date with a continuous list of dates, one row per day.
Then your measure could look like this:
PreviousMonthCount=
CALCULATE (
COUNTROWS ( 'ws pds' ),
'ws pds'[Month Tagged] <> BLANK (),
PARALLELPERIOD ( Calendar[Date], -1, MONTH )
)

Filter on date stored in integer fields without datetime

I've got two columns, one with a year (e.g. 2013, 2014) and one with a number of the month (e.g. 2, 3, 4).
Is there code that I could use to bring back the previous 12 months from my selected month? So if I selected my year as '2014' and my month as '9', I'd like to bring back the results going back to year '2013' and month '10'.
Is that possible without having a datetime field?
This should be possible. I would do this with "month" arithmetic. For instance, to get all dates since 2013-10, for selected month 2014-09:
select t.*
from table t
where year*12 + month >= 2014*12 + 9 - 12;