I have found this great solution from an old stack post: https://stackoverflow.com/a/6871572/194653
But for some reason it is not working for me, this is the query that I need to execute to get the max value:
Select a.id as id, first_name as name, surname, h.maiden_name, c.salutation, c.gender, coalesce (d.marital_status,’Z’) as marital_status, coalesce (g.occupation, ‘0047’) as occupation, coalesce (f.kind_of_work, '9') as kind_of_work,‘’ as change_type
(SELECT Max (date_last_mod)
FROM (VALUES (a.date_last_mod), (c.date_last_mod),(d.date_last_mod),
(f.date_last_mod), (g.date_last_mod), (h.date_last_mod)) AS value (date_last_mod)) as date_last_mod
from people.al_names_spk a
inner join people.al_salutation_spk c
on a.id = c.id
left join people.al_marital_status_spk d
on a.id = d.id
left join people.al_kind_of_work_spk f
on a.id = f.id
inner join people.al_occupation_spk g
on a.id = a.id
left join people.al_maiden_name_spk h
a.id = h.id
LEFT SEMI JOIN people_st.stg_person_hist_spk b ON (a.id = b.id AND b.data_date_part = '2023-06-01’)
I have also tried this other possible solution to select the max values but it didn't work:
(SELECT MAX(date_last_mod) FROM (
SELECT a.date_last_mod
UNION ALL
SELECT c.date_last_mod
UNION ALL
SELECT d.date_last_mod
UNION ALL
SELECT f.date_last_mod
UNION ALL
SELECT g.date_last_mod
UNION ALL
SELECT h.date_last_mod ) AS value(date_last_mod)) AS date_last_mod
Am I doing something wrong? Everytime I execute this I have a syntax error message. I have to say that I am executing this in HUE.
UPDATE: The error message:
AnalysisException: Syntax error in line 37:undefined: (SELECT Max(date_last_mod) ^ Encountered: SELECT Expected: ALL, CASE, CAST, DEFAULT, DISTINCT, EXISTS, FALSE, IF, INTERVAL, LEFT, NOT, NULL, REPLACE, RIGHT, TRUNCATE, TRUE, IDENTIFIER CAUSED BY: Exception: Syntax error
Thanks for reading.