I have data related material transactions in one table and log history header data related to materials is in another table and detailed log history data in third table. I'm trying to get different status update dates matched to material table but I get duplicate rows for one material transaction
Original material transaction table:
| ORDER_NO | MATERIAL | QTY |
|---|---|---|
| 0001 | MAT01 | 2 |
| 0002 | MAT02 | 5 |
Original Log History Header transaction table:
| ORDER_NO | LOG_ID |
|---|---|
| 0001 | 1001 |
| 0001 | 1002 |
Status code 1 refers to Opened and code 2 to Closed
Detailed Log History table:
| LOG_ID | STATUS_CODE | DATE |
|---|---|---|
| 1001 | 1 | 11/12/2021 |
| 1002 | 2 | 15/12/2021 |
With following SQL query:
SELECT
TO_CHAR (m.order_no) order_no,
m.material,
a.date opened_date,
ab.closed_date
FROM MATERIAL_TRANSACTIONS m
INNER JOIN HISTORY_LOG t
ON m.ORDER_NO = t.ORDER_NO
INNER JOIN HISTORY_LOG_DETAILED a
ON t.LOG_ID = a.LOG_ID
AND a.STATUS_CODE = '1'
INNER JOIN HISTORY_LOG_DETAILED ab
ON t.LOG_ID = ab.LOG_ID
AND ab.STATUS_CODE = '2'
I get following result:
| ORDER_NO | MATERIAL | QTY | OPENED_DATE | CLOSED_DATE |
|---|---|---|---|---|
| 0001 | MAT01 | 2 | 11/12/2021 | |
| 0001 | MAT01 | 2 | 15/12/2021 |
And I would like to get the status dates to the same row as below:
| ORDER_NO | MATERIAL | QTY | OPENED_DATE | CLOSED_DATE |
|---|---|---|---|---|
| 0001 | MAT01 | 2 | 11/12/2021 | 15/12/2021 |
I would appreciate all the help I can get and am very sorry if there already is topic for similar issue.