SELECT
tab2.os_key,
tab1.tablename,
tab1.suiffix,
'DROP TABLE "' || tab1.tablename || '";' AS drop_table
FROM
fd_content_dataos AS tab2
JOIN (
SELECT
tablename,
REPLACE ( tablename, '_', '__' ),
SPLIT_PART( tablename, '_', LENGTH ( REPLACE ( tablename, '_', '__' )) - LENGTH ( tablename ) + 1 ) AS suiffix
FROM
pg_tables
WHERE
tablename LIKE'fd_%'
) AS tab1 ON tab2.os_key = tab1.suiffix
WHERE
tab2.os_key IN ( 'CYAE9KBWUN', 'Z74NG1YOGU', 'EUWUUBXVTC', 'INCS7DC3DY', 'AF3NSIWP4X', '56RAXZ7ZOT', 'QRPRBMINPP', '4XQY9BPXMN' )
ORDER BY
tab2.os_key;
SELECT
tab1.tablename,
tab1.suffix
FROM
(
SELECT
tablename,
split_part( tablename, '_', LENGTH ( REPLACE ( tablename, '_', '__' )) - LENGTH ( tablename ) + 1 ) AS suffix
FROM
pg_tables
WHERE
tablename LIKE'fd_%'
) AS tab1
WHERE
tab1.suffix = UPPER ( tab1.suffix )
AND LENGTH ( tab1.suffix ) = 10
AND tab1.suffix NOT IN ( SELECT os_key FROM fd_content_dataos )
ORDER BY
tab1.suffix;