postgre如何联接/合并SQL中不同表中的日期列



我有三个不同的CTE(a、b、c(,它们有列date、id和value。只有值列中有不同的值。日期和Id中的值将相同。

select a.date,a.id,a.value,b.date,b.id,b.value,c.date,c.id,c.value
from table_a a
full outer join table_b on b.id = a.id
full outer join table_c on c.id = b.id 

上面的代码在这里给出了以下输出设备图像描述

我想要以下输出,其中日期列被合并并用作索引在此处输入图像描述

如何获得第二张图片中的输出。

使用coalesce,通常在使用full join时使用

select 
coalesce(a.date, b.date, c.date) as ddate,
a.id,a.value,
b.id,b.value,
c.id,c.value
from table_a a
full outer join table_b on b.id = a.id
full outer join table_c on c.id = b.id 

最新更新