我有以下查询:
select id, table1.date1, table2.date2, table1.name
from table1
join table2 using (id)
Run Code Online (Sandbox Code Playgroud)
我还想有另一列,MAX(table1.date1, table2.date2)但我没有找到正确的语法。我不希望 MAX 遍历表中的所有行并取 MAX() 我希望它从行中指定的两个值中选择最大值。
例子:
id date1 date2 name max
1 2020-01-01 2020-04-01 A 2020-04-01
2 2019-02-01 2020-01-03 B 2020-01-03
3 2019-02-01 null c 2019-02-01
Run Code Online (Sandbox Code Playgroud)
我也不能分组,因为我不想在这里分组任何东西。更类似于coalesce 给出值的函数列表并从中选择最大值
Gor*_*off 10
使用greatest():
select id, t1.date1, t2.date2, t1.name,
greatest(t1.date1, t2.date2)
from table1 t1 join
table2 t2
using (id);
Run Code Online (Sandbox Code Playgroud)
请注意,如果任何参数为,则greatest()返回。所以,如果你有价值观,你就需要特别照顾。NULLNULLNULL