0

我有 2 个复杂查询,它们都是 postgres 中的子查询,其结果是:

q1_results = id , delta , metric_1
q2_results = id , delta , metric_2

我想结合查询的结果,所以外部查询可以访问:

results_a = id , delta , metric_1 , metric_2
results_b = id , delta , combined_metric

我不知道该怎么做。在线搜索不断将我引向UNION,但这使指标保持在同一列中。我需要让他们分开。

4

1 回答 1

1

尚不清楚您在问题和评论中要问什么,但听起来您可能正在寻找与一堆合并语句的完全连接,例如:

-- create view at your option, e.g.:
-- create view combined_query as
select coalesce(a.id, b.id) as id,
       coalesce(a.delta, b.delta) as delta,
       a.metric1 as metric1,
       b.metric2 as metric2,
       coalesce(a.metric1,0) + coalesce(b.metric2,0) as combined
from   (...) as results_a a
full join (...) as results_b b on a.id = b.id -- and a.delta = b.delta maybe?
于 2013-09-26T21:24:11.403 回答