我有3张桌子:
with current_exclusive as(
select id_station, area_type,
count(*) as total_entries
from c1169.data_cashier
where id_station IN(2439,2441,2443,2445,2447,2449) and date >= '2017-10-30' and date <= '2017-12-30'
group by id_station, area_type
), current_table as(
select id_station, area_type,
sum(total_time) filter (where previous_status = 1) as total_time
from c1169.data_table
where id_station IN(2439,2441,2443,2445,2447,2449) and date >= '2017-10-30' and date < '2017-12-30'
group by id_station, area_type
), current_cashier as(
select id_station, area_type,
sum(1) as total_transactions
from c1169.data_cashier
where id_station IN(2439,2441,2443,2445,2447,2449) and date >= '2017-10-30' and date < '2017-12-30'
group by id_station, area_type
)
select *
from current_exclusive
full join current_table on current_exclusive.id_station = current_table.id_station and current_exclusive.area_type = current_table.area_type
full join current_cashier on current_exclusive.id_station = current_cashier.id_station and current_exclusive.area_type = current_cashier.area_type
但我的预期结果是:
有没有办法选择*并显示预期的结果?因为当我进行完全连接时,id_station和area_type在某些表中可能为null,因此很难选择哪个列不为null。
比如:select case id_station is not null then id_station else id_station1 end
,但我有多达10个表,所以在select case
答案 0 :(得分:0)
如果您坚持使用select *
,则无法完成任何操作,因为您从不同的表中获取值。
您拥有的选项是添加COALESCE
块,该列为您提供列列表中的第一个non-null
值。
所以,你可以使用。
select COALESCE( current_exclusive.id_station, current_table.id_station, current_cashier.id_station ) as id_station ,
COALESCE( current_exclusive.area_type , current_table.area_type, current_cashier.area_type ) as area_type ,.....
...
from current_exclusive
full join current_table..
...
答案 1 :(得分:0)
USING
使用(join_column [,...])
USING(a,b,...)形式的一个子句是ON left_table.a = right_table.a和left_table.b = right_table.b ....的简写。另外,USING意味着每个只有一个一对等效列将包含在连接输出中,而不是两者。
select *
from current_exclusive
full join current_table using (id_station, area_type)
full join current_cashier using (id_station, area_type)