Site icon Bolhai Mobiliaria

postgresql – case when to get the max timestamp between two columns

I want to return the max updated_date timestamp. I have this code:

select 
case when max(t1.updated_date) > max(t2.updated_date)
then t1.updated_date else t2.updated_date
end as MaxDate
from table1 t1
inner join table2 t2
on t1.id = t2.id 
where t1.id = '5'
group by t1.updated_date, t2.updated_date 

When I run this code, my result set is both the max updated_date from t1 AND t2:

MaxDate
2021-12-10 8:00:00
2021-12-20 23:00:00

Why is it returning both? How do I get it to return only 2021-12-20 23:00:00 (i.e the max timestamp when comparing the two columns)?

Exit mobile version