Goodnight,
I have a single table that has duplicate lines, but each line has information that the other does not have, I need to merge the information from the 2 lines into a single line always with the maximum date information.
I tried some WITH and sub selects performing a CONCAT to create a unique key but I always get 2 lines back.
Example
Table
ANO | MES | DOC | CURRENCY | Nº| COUNTRY | BAN
2022 | 03 | 0392 | NULL | NULL |BR |NULL
2022 | 04 | 0392 | BRL | 0398 | BR | BR10
Needed
ANO | MES | DOC | CURRENCY | Nº| COUNTRY | BAN
2022 | 04 | 0392 | BR | 0398 | BR | BR10
My query and Result
2022040392| 2022 | 04 | 0392 | BRL | 0398 |BR | BR10
2022040392| 2022 | 04 | 0392 | BRL | 0398 | BR | BR10
SELECT DISTINCT
CONCAT (ANO,MES,DOC) AS X,
MES,
ANO,
FROM XPTO
LEFT JOIN (
SELECT DISTINCT
CONCAT (ANO,MES,DOC) AS X,
MAX(MES),
MAX(ANO)
FROM XPTO
WHERE DOC = 'X'
GROUP BY
ANO,
MES) t2 on
t2.x = CONCAT (ANO,MES,DOC)
LEFT JOIN (
SELECT DISTINCT
CONCAT (ANO,MES,DOC) AS X,
MAX(MES),
MAX(ANO)
FROM XPTO
WHERE DOC = 'X'
GROUP BY
ANO,
MES) t1 on
t1.x = CONCAT (ANO,MES,DOC)