全外连接的替代方式

Alternative way of full outer join

我正在运行查询

select * from
(select name, count(distinct id) as ids, date 
from table1
group by name, date ) as tt
full outer join
(select st_name as name,count(distinct id) as ids, date 
from table2  
group by st_name, date) as ts
on tt.name= ts.name
and tt.ids = ts.ids

它 运行 成功了,但我想问一下是否有其他更有效的方法来 运行 此查询。

我假设您希望获得两个数字不相同的天数(这似乎是您希望从此类查询中得到的最合理结果)。所以,这解决了这个问题。

FULL OUTER JOIN 应该没问题。但另一种方法是尝试 UNION ALL 和聚合:

select name, sum(ids_1), sum(ids_2), date
from ((select name, count(distinct id) as ids_1, NULL as ids_2, date 
       from table1
       group by name, date
      ) 
      union all
      (select st_name as name, NULL, count(distinct id) as ids_2, date 
       from table2  
       group by st_name, date
      )
     ) 
group by name, date
having sum(ids_1) = sum(ids_2)