如果我可以从表格栏中执行此查询:
SELECT count(distinct bar_id) as count,
date_trunc('day', created_at) as date
FROM
bars
GROUP BY
date
ORDER BY
date desc
来自表foos的这个查询:
SELECT count(distinct foo_id),
date_trunc('day', created_at) as date
FROM
foos
GROUP BY
date
ORDER BY
date desc
我得到了很好的清单,如:
22, 2017-09-14
31, 2017-09-13
18, 2017-09-12
和此:
34, 2017-09-14
55, 2017-09-13
67, 2017-09-12
在一个查询中,如何将这两者结合起来产生:
22/34, 0.64, 2017-09-14
31/55, 0.56, 2017-09-13
18/67, 0.26, 2017-09-12
即。我想将table1中的每个计数除以按日分组的table2中的日期。 22/34 = 0.64,31 / 55 = 0.56等。
答案 0 :(得分:2)
你可以加入他们:
SELECT bars.counts * 1.0 / foos.counts
, bars.date
FROM
(
SELECT count(distinct bar_id) as counts,
date_trunc('day', created_at) as date
FROM
bars
GROUP BY
date
) bars_q
JOIN
(
SELECT count(distinct foo_id) as counts,
date_trunc('day', created_at) as date
FROM
foos
GROUP BY
date
) foos_q
ON bars_q.date = foos_q.date
答案 1 :(得分:0)
您可以使用FULL OUTER JOIN检索两个表上的记录,还可以显示没有匹配日期的记录
{{1}}