WITH tabe_ab AS (
SELECT * FROM VALUES
(9848, 923, '2019-09-14 21:09'::timestamp_ntz),
(3242, 427, '2019-09-15 21:09'::timestamp_ntz),
(7845, 514, '2019-09-16 21:09'::timestamp_ntz)
d(AP_ID, UPDT_BY_ID, UPDT_DATE)
), table_bc AS (
SELECT * FROM VALUES
(1234, 123, '2019-08-21 20:30'::timestamp_ntz),
(5678, 456, '2019-08-22 20:30'::timestamp_ntz),
(2468, 789, '2019-08-23 20:30'::timestamp_ntz)
b(AP_ID, CREATED_BY_ID, CREATED_DT)
), data_union AS (
SELECT 'AB' as table_name
,AP_ID
,UPDT_BY_ID
,UPDT_DATE as SRC_DATE
FROM tabe_ab
UNION ALL
SELECT 'BC' as table_name
,AP_ID
,CREATED_BY_ID AS UPDT_BY_ID
,CREATED_DT as SRC_DATE
FROM table_bc
)
SELECT table_name
,OBJECT_CONSTRUCT('AP_ID',AP_ID,'UPDT_BY_ID',UPDT_BY_ID) AS id_details
,SRC_DATE
,CURRENT_DATE
FROM data_union
order by 1,3;
дает результаты:
TABLE_NAME ID_DETAILS SRC_DATE CURRENT_DATE
AB { "AP_ID": 9848, "UPDT_BY_ID": 923 } 2019-09-14 21:09:00.000 2020-03-29
AB { "AP_ID": 3242, "UPDT_BY_ID": 427 } 2019-09-15 21:09:00.000 2020-03-29
AB { "AP_ID": 7845, "UPDT_BY_ID": 514 } 2019-09-16 21:09:00.000 2020-03-29
BC { "AP_ID": 1234, "UPDT_BY_ID": 123 } 2019-08-21 20:30:00.000 2020-03-29
BC { "AP_ID": 5678, "UPDT_BY_ID": 456 } 2019-08-22 20:30:00.000 2020-03-29
BC { "AP_ID": 2468, "UPDT_BY_ID": 789 } 2019-08-23 20:30:00.000 2020-03-29
для ваших реальных таблиц вы просто используете секцию CTE
WITH data_union AS (
SELECT 'AB' as table_name
,AP_ID
,UPDT_BY_ID
,UPDT_DATE as SRC_DATE
FROM ab
UNION ALL
SELECT 'BC' as table_name
,AP_ID
,CREATED_BY_ID AS UPDT_BY_ID
,CREATED_DT as SRC_DATE
FROM bc
)