Create a list of months between two dates
I have a dataset with two date columns: start_date and last_invoice_date for each client. I would like to generate a list of the months between these two dates (not the number of months, but the actual month names).
For example, if I have:
client start_date last_invoice_date
A Mar 1, 2020 Jun 1, 2020
B January 10, 2018 May 10, 2020
I´d like to see this:
client start_date last_invoice_date date
A Mar 1, 2020 Jun 1, 2020 Mar 2020
A Mar 1, 2020 Jun 1, 2020 Apr 2020
A Mar 1, 2020 Jun 1, 2020 May 2020
A Mar 1, 2020 Jun 1, 2020 Jun 2020
........ Same for client B and so on.
I´m trying this in sql dataflow, but I kow the logic is wrong.
SELECT a.*, DATE_ADD(`start_date`, INTERVAL @i:=@i+1 MONTH) AS 'Date'
FROM `table` a
, (SELECT @i=0) b
HAVING @i< PERIOD_DIFF(`last_invoice_date`,`start_date`)
Is there another approach worth a try?
Best Answer
-
you need a table with a list of numbers or a table with a list of dates (a date dimension would be easier)
Then you can...
SELECT
f.*
FROM
Transaction t
JOIN
Date d
ON
d.date BETWEEN t.StartDate and t.EndDate
If you only want months then do a subquery
JOIN
(SELECT DISTINCT lastday(date) as date FROM date GROUP BY lastday(date)
Just be careful what happens if the contract wasn't active on the last day of the month? If you want to keep the value then alter your JOIN clause use lastday(enddate).
If you're using numbers then
JOIN numbers
ON date_add(startDate, interval n.num month) <= endDate
Again... test what happens at the boundary months and adjust as appropriate.
Jae Wilson
Check out my 🎥 Domo Training YouTube Channel 👨💻
**Say "Thanks" by clicking the ❤️ in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1
Answers
-
you need a table with a list of numbers or a table with a list of dates (a date dimension would be easier)
Then you can...
SELECT
f.*
FROM
Transaction t
JOIN
Date d
ON
d.date BETWEEN t.StartDate and t.EndDate
If you only want months then do a subquery
JOIN
(SELECT DISTINCT lastday(date) as date FROM date GROUP BY lastday(date)
Just be careful what happens if the contract wasn't active on the last day of the month? If you want to keep the value then alter your JOIN clause use lastday(enddate).
If you're using numbers then
JOIN numbers
ON date_add(startDate, interval n.num month) <= endDate
Again... test what happens at the boundary months and adjust as appropriate.
Jae Wilson
Check out my 🎥 Domo Training YouTube Channel 👨💻
**Say "Thanks" by clicking the ❤️ in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 753 Beast Mode
- 61 App Studio
- 41 Variables
- 692 Automate
- 177 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive