我有两个包含一些分析数据的表。
table a_2_bb_2_ccc
id_a
id_bb
id_ccc
table a_2_bb_2_dddd
id_a
id_bb
id_dddd
我试图找到一种好方法来定期将每个表中的三元组计数缓存到由共享的两个元素键入的统一第三个表中:
table a_2_bb_cache
id_a
id_bb
count_ccc
count_dddd
但是,我无法绕过这个。
我一直在考虑尝试使用单表分组和合并来解决这个问题:
CREATE TEMP TABLE a_2_bb_2_ccc__count AS
SELECT
id_a,
id_bb,
COUNT(id_ccc) AS count_ccc
FROM
a_2_bb_2_ccc
GROUP BY id_a, id_bb
;
CREATE TEMP TABLE a_2_bb_2_dddd__count AS
SELECT
id_a,
id_bb,
COUNT(id_dddd) AS count_dddd
FROM
a_2_bb_2_dddd
GROUP BY id_a, id_bb
;
然而,我无法确定任何给定(id_a,id_bb)组合在一个或两个表中。所以我加入的所有尝试都给了我错误的数据。
有人能指出我正确的方向吗?
答案 0 :(得分:1)
我想你想要full outer join
select
id_a,
id_bb,
CASE WHEN count_ccc IS NOT NULL count_ccc ELSE 0 END,
CASE WHEN count_dddd IS NOT NULL count_dddd ELSE 0 END
from
(
select
id_a,
id_bb,
count(id_ccc) as count_ccc
from a_2_bb_2_ccc
group by id_a, id_bb
) sq1
full outer join
(
select
id_a,
id_bb,
count(id_dddd) as count_dddd
from a_2_bb_2_dddd
group by id_a, id_bb
) sq2 using(id_a, id_bb);
using
子句简化了它,因为它使参数明确无效。否则,在使用on
时,有必要coalesce
他们。