I have query which works fine for all my requirements apart from the below scenario
For a particular ID, based on the FST_NM, MDL_NM, LST_NM and SFX_NM combination which ever is occurring frequently then those should be pulled. In the below case when all the names are not null then that values should be pulled but right now the query is pulling FST_NM as TES , MDL_NM as TAM , LST_NM as Null and SFX_NM as NULL.
Actually it should be TES, TAM, BAM, SET.
It would be helpful if you can help me to acheive this.
ID | FST_NM | MDL_NM | LST_NM | SFX_NM |
1 | TES | TAM | BAM | SET |
1 | TES | TAM | | |
1 | TES | TAM | | |
1 | TES | TAM | | |
SELECT FST_NM,
MDL_NM,
LST_NM,
SFX_NM,
ID
FROM (SELECT ID,
UPPER(TRIM(FST_NM)) FST_NM ,
UPPER(TRIM(MDL_NM)) MDL_NM,
UPPER(TRIM(SFX_NM)) SFX_NM,
UPPER(TRIM(LST_NM)) LST_NM,
ROW_NUMBER () OVER ( PARTITION BY ID
ORDER BY COUNT (*) DESC
) AS TRANK
FROM TEST
GROUP BY ID,UPPER(TRIM(FST_NM)),
UPPER(TRIM(MDL_NM)),
UPPER(TRIM(SFX_NM)),
UPPER(TRIM(LST_NM)))
WHERE TRANK=1
Thanks