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
Related
I have a table where the data updates every hour and I need to subtract the number of quantities as of today and what was 60 days back. How can that be done in 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
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 )
)
I need to build a report which has week's sales data by department by date (which I have done using Matrix) and compare it to weeks sales last year. Report would be run weekly. I am wondering which approach would be most efficient:
1) generate separate data sets - 1) for 1 weeks data and 2) for 1
week a year ago and then compare these values;
2) create 1 data set for a period of 1 year + 1 week and insert
calculated fields in the data set;
3) create 1 data set for a period of 1 year + 1 week and insert
calculated fields with expressions inside the report
4) any other.
Thanks
If you tables are well indexed, then I'd suggest you go with the first approach - It'd reduce the data to be compared. I feel that pulling data from one year in past would be a less expensive than comparing 2 week's data out of one year's data.
You could use the week number of any given week to efficiently pull data for any week based on the date range.
I am wanting to do some queries on a sales table and a purchases table, things like showing the total costs, or total sales of a particular item etc.
Both of these tables also have a date field, which stores dates in sortable format(just the default I suppose?)
I am wondering how I would do things with this date field as part of my query to use date ranges such as:
The last year, from any given date of the year
The last 30 days, from any given day
To show set months, such as January, Febuary etc.
Are these types of queries possible just using a DATE field, or would it be easier to store months and years as separate tex fields?
If a given DATE field MY_DATE, you can perform those 3 operation using various date functions:
1. Select last years records
SELECT * FROM MY_TABLE
WHERE YEAR(my_date) = YEAR(CURDATE()) - 1
2. Last 30 Days
SELECT * FROM MY_TABLE
WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) < MY_DATE
3. Show the month name
SELECT MONTHNAME(MY_DATE), * FROM MY_TABLE
I have always found it advantageous to store dates as Unix timestamps. They're extremely easy to sort by and query by range, and MySQL has built-in features that help (like UNIX_TIMESTAMP() and FROM_UNIXTIME()).
You can store them in INT(11) columns; when you program with them you learn quickly that a day is 86400 seconds, and you can get more complex ranges by multiplying that by a number of days (e.g. a month is close enough to 86400 * 30, and programming languages usually have excellent facilities for converting to and from them built into standard libraries).