使用generate_series从两个表中选择计数

时间:2019-06-15 10:35:54

标签: postgresql

我有两个表,问与答。我想从带有generate_series月的表中选择两个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
                        , 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

0 个答案:

没有答案