Self Join Outer JOIN not working as expected
Hi,
I am trying to do Outer Join on the same table. However I am not getting the result that is expected.
I have the following dataset:
Item | Country | Event Date | Event | Event Order |
A | US | Apr 29, 2019 | Out Of Stock | 1 |
A | US | May 13, 2019 | Back In Stock | 2 |
A | US | Oct 14, 2019 | Out Of Stock | 3 |
A | UK | Jan 28, 2019 | Out of Stock | 1 |
A | UK | Feb 04, 2019 | Back IN Stock | 2 |
A | UK | May 06, 2019 | Out of stock | 3 |
I am trying to do a SELF JOIN OUTER join to get the date range for which the Item is Out of Stock or In Stock.
Ideally this is the desired O/P:
Item | Country | Event Order | Status | Start Date | End Date |
A | US | 1 | Out Of Stock | Apr 29, 2019 | May 13, 2019 |
A | US | 2 | Back In Stock | May 13, 2019 | Oct 14,2019 |
A | US | 3 | Out of Stock | Oct 14, 2019 | - |
A | UK | 1 | Out of stock | Jan 28, 2019 | Feb 04, 2019 |
A | UK | 2 | Back in stock | Feb 04, 2019 | May 06, 2019 |
A | UK | 3 | Out of stock | May 06, 2019 | - |
This is my code to achieve the desired o/p above:
SELECT a.`Item`,
a.`Country`,
a.`Event Order`,
a.`Event` as 'Status',
a.`Event Date` as 'Start Date',
b.`Event Date` as 'End Date'
FROM `input_table` a
LEFT JOIN `input_table` b
ON b.`Event Order` = a.`Event Order` + 1 and a.`Item` = b.`Item` and a.`Country` = b.`Country`
However, this is the o/p i am getting:
Item | Country | Event Order | Status | Start Date | End Date |
A | US | 1 | Out Of Stock | Apr 29, 2019 | May 13, 2019 |
A | US | 2 | Back In Stock | May 13, 2019 | Oct 14,2019 |
A | UK | 1 | Out of stock | Jan 28, 2019 | Feb 04, 2019 |
A | UK | 2 | Back in stock | Feb 04, 2019 | May 06, 2019 |
Since this is a LEFT OUTER join, it should fetch all rows from the LEFT TABLE even if it does not find matching rows in the RIGHT TABLE. In this example, Event Order = 3 exists in the LEFT TABLE but Event Order = 4 does not exist in the RIGHT TABLE.
Are my conditions in the joins right? Am I missing out on anything?
Thanks in advance.
Best Answer
-
Never mind. It is working fine.
The rows that I thought were missing were in a different order in the output.
0
Answers
-
Never mind. It is working fine.
The rows that I thought were missing were in a different order in the output.
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 738 Beast Mode
- 57 App Studio
- 40 Variables
- 685 Automate
- 176 Apps
- 452 APIs & Domo Developer
- 47 Workflows
- 10 DomoAI
- 36 Predict
- 15 Jupyter Workspaces
- 21 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 124 Manage
- 121 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive