Joining and getting all dates between start and end date
Im doing a join on billing ID's. Each billing ID has a start date and an end date. Im trying to return all dates between start and end date for each billing ID. I tried adding this formula to join.
dt
BETWEEN Service Start Date
and Service End Date
It isn't working as expected. Im just getting one date for each billing ID
Best Answers
-
Here is some SQL that will build a sequential list of days for a preset set of years. You can then LEFT JOIN against your data to set the values for each date:
WITH years AS (
SELECT 2020 AS year UNION SELECT 2021 AS year UNION SELECT 2023 AS year UNION SELECT 2024 AS year
),
months AS (
SELECT 1 AS month UNION SELECT 2 AS month UNION SELECT 3 AS month UNION SELECT 4 AS month UNION
SELECT 5 AS month UNION SELECT 6 AS month UNION SELECT 7 AS month UNION SELECT 8 AS month UNION
SELECT 9 AS month UNION SELECT 10 AS month UNION SELECT 11 AS month UNION SELECT 12 AS month
),
days AS (
SELECT 1 AS day UNION SELECT 2 AS day UNION SELECT 3 AS day UNION SELECT 4 AS day UNION SELECT 5 AS day UNION
SELECT 6 AS day UNION SELECT 7 AS day UNION SELECT 8 AS day UNION SELECT 9 AS day UNION SELECT 10 AS day UNION
SELECT 11 AS day UNION SELECT 12 AS day UNION SELECT 13 AS day UNION SELECT 14 AS day UNION SELECT 15 AS day UNION
SELECT 16 AS day UNION SELECT 17 AS day UNION SELECT 18 AS day UNION SELECT 19 AS day UNION SELECT 20 AS day UNION
SELECT 21 AS day UNION SELECT 22 AS day UNION SELECT 23 AS day UNION SELECT 24 AS day UNION SELECT 25 AS day UNION
SELECT 26 AS day UNION SELECT 27 AS day UNION SELECT 28 AS day UNION SELECT 29 AS day UNION SELECT 30 AS day UNION
SELECT 31 AS day
),
all_dates AS (
SELECT DATE(STR_TO_DATE(CONCAT(years.year, '-', months.month, '-', days.day), '%Y-%m-%d')) AS day
FROM years
CROSS JOIN months
CROSS JOIN days
WHERE DATE(STR_TO_DATE(CONCAT(years.year, '-', months.month, '-', days.day), '%Y-%m-%d')) IS NOT NULL
)
SELECT day
FROM all_dates
WHERE day BETWEEN '2021-04-30' AND '2024-10-23'
ORDER BY day0 -
Using magic ETL, take the date dimension dataset (Domo Dimensions connector) and feed it into a Add Constant tile to create a new field called "join column" with a value of 1. Do the same for your input dataset. Feed both of those into the join based on the join column. Then you feed that into a filter field with your BETWEEN clause. The joining on the join column will do a cartesian join and add all dates for every record then you're filtering out the rows where the dates from the dimension dataset aren't within your range.
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**1
Answers
-
Here is some SQL that will build a sequential list of days for a preset set of years. You can then LEFT JOIN against your data to set the values for each date:
WITH years AS (
SELECT 2020 AS year UNION SELECT 2021 AS year UNION SELECT 2023 AS year UNION SELECT 2024 AS year
),
months AS (
SELECT 1 AS month UNION SELECT 2 AS month UNION SELECT 3 AS month UNION SELECT 4 AS month UNION
SELECT 5 AS month UNION SELECT 6 AS month UNION SELECT 7 AS month UNION SELECT 8 AS month UNION
SELECT 9 AS month UNION SELECT 10 AS month UNION SELECT 11 AS month UNION SELECT 12 AS month
),
days AS (
SELECT 1 AS day UNION SELECT 2 AS day UNION SELECT 3 AS day UNION SELECT 4 AS day UNION SELECT 5 AS day UNION
SELECT 6 AS day UNION SELECT 7 AS day UNION SELECT 8 AS day UNION SELECT 9 AS day UNION SELECT 10 AS day UNION
SELECT 11 AS day UNION SELECT 12 AS day UNION SELECT 13 AS day UNION SELECT 14 AS day UNION SELECT 15 AS day UNION
SELECT 16 AS day UNION SELECT 17 AS day UNION SELECT 18 AS day UNION SELECT 19 AS day UNION SELECT 20 AS day UNION
SELECT 21 AS day UNION SELECT 22 AS day UNION SELECT 23 AS day UNION SELECT 24 AS day UNION SELECT 25 AS day UNION
SELECT 26 AS day UNION SELECT 27 AS day UNION SELECT 28 AS day UNION SELECT 29 AS day UNION SELECT 30 AS day UNION
SELECT 31 AS day
),
all_dates AS (
SELECT DATE(STR_TO_DATE(CONCAT(years.year, '-', months.month, '-', days.day), '%Y-%m-%d')) AS day
FROM years
CROSS JOIN months
CROSS JOIN days
WHERE DATE(STR_TO_DATE(CONCAT(years.year, '-', months.month, '-', days.day), '%Y-%m-%d')) IS NOT NULL
)
SELECT day
FROM all_dates
WHERE day BETWEEN '2021-04-30' AND '2024-10-23'
ORDER BY day0 -
Using magic ETL, take the date dimension dataset (Domo Dimensions connector) and feed it into a Add Constant tile to create a new field called "join column" with a value of 1. Do the same for your input dataset. Feed both of those into the join based on the join column. Then you feed that into a filter field with your BETWEEN clause. The joining on the join column will do a cartesian join and add all dates for every record then you're filtering out the rows where the dates from the dimension dataset aren't within your range.
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**1 -
@hanmari - that second line should be:
SELECT 2020 AS year UNION SELECT 2021 AS year UNION SELECT 2022 AS year UNION SELECT 2023 AS year UNION SELECT 2024 AS year** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0
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