Postgresql 使用generate_series从两个表中选择count
我有两张桌子,问答。我想从这些表中选择两个count(*),每个月生成一个系列Postgresql 使用generate_series从两个表中选择count,postgresql,Postgresql,我有两张桌子,问答。我想从这些表中选择两个count(*),每个月生成一个系列 SELECT to_char(s.tag,'yyyy-mm') AS month , count(t.id) AS questions_count FROM ( SELECT generate_series(min(created_time)::date , current_date::date , in
SELECT to_char(s.tag,'yyyy-mm') AS month
, count(t.id) AS questions_count
FROM (
SELECT generate_series(min(created_time)::date
, current_date::date
, interval '1 month'
)::date AS tag
FROM core.questions t
where t.created_by=2
) s
LEFT JOIN core.questions t ON t.created_time::date = s.tag
GROUP BY 1
ORDER BY 1;
我想从这样的两个表中得到一个结果
month questions_count answer_count
"2006-12" | 4 | 1
"2007-01" | 1 | 2
"2007-02" | 1 | 3
透视/交叉选项卡/条件聚合