Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
postgresql-如何在聚合和联接的表输出中显示空值?_Postgresql - Fatal编程技术网

postgresql-如何在聚合和联接的表输出中显示空值?

postgresql-如何在聚合和联接的表输出中显示空值?,postgresql,Postgresql,我有以下两个表格: select * from tokens; id | datefield | category | amount ----+---------------------+----------+-------- 1 | 2019-01-01 00:00:00 | 1 | 3 4 | 2019-02-01 00:00:00 | 1 | 3 2 | 2019-01-15 00:00:00 |

我有以下两个表格:

select * from tokens;
 id |      datefield      | category | amount 
----+---------------------+----------+--------
  1 | 2019-01-01 00:00:00 |        1 |      3
  4 | 2019-02-01 00:00:00 |        1 |      3
  2 | 2019-01-15 00:00:00 |        2 |      1
  5 | 2019-01-25 00:00:00 |        3 |      2
  3 | 2019-02-17 00:00:00 |        2 |      1
  6 | 2019-03-16 00:00:00 |        1 |      5
  7 | 2019-03-11 00:00:00 |        3 |      6

select * from category;
 id |   name    
----+-----------
  1 | fiction
  2 | sports
  3 | news
  name   | sum | monthfield 
---------+--------+------------
 fiction |   3    | 2019-01
 sports  |   1    | 2019-01
 news    |   2    | 2019-01
 fiction |   3    | 2019-02
 sports  |   1    | 2019-02
 news    |   NULL | 2019-02
 fiction |   5    | 2019-03
 sports  |   NULL | 2019-03
 news    |   6    | 2019-03
现在,我想加入这些表,并在表中显示每个类别每月的总金额,如下所示:

select * from tokens;
 id |      datefield      | category | amount 
----+---------------------+----------+--------
  1 | 2019-01-01 00:00:00 |        1 |      3
  4 | 2019-02-01 00:00:00 |        1 |      3
  2 | 2019-01-15 00:00:00 |        2 |      1
  5 | 2019-01-25 00:00:00 |        3 |      2
  3 | 2019-02-17 00:00:00 |        2 |      1
  6 | 2019-03-16 00:00:00 |        1 |      5
  7 | 2019-03-11 00:00:00 |        3 |      6

select * from category;
 id |   name    
----+-----------
  1 | fiction
  2 | sports
  3 | news
  name   | sum | monthfield 
---------+--------+------------
 fiction |   3    | 2019-01
 sports  |   1    | 2019-01
 news    |   2    | 2019-01
 fiction |   3    | 2019-02
 sports  |   1    | 2019-02
 news    |   NULL | 2019-02
 fiction |   5    | 2019-03
 sports  |   NULL | 2019-03
 news    |   6    | 2019-03
我已经编写了以下查询,但是我无法获得具有空值的行,如上述所需输出所示:

select
  c.name, sum(t.amount), to_char(t.datefield, 'YYYY-MM') as monthfield
from
  category c
FULL OUTER JOIN
  tokens t on c.id = t.category
group by
  (c.name, c.id, monthfield)
order by
  (to_char(t.datefield, 'YYYY-MM'),c.id)
  ;

  name   | sum | monthfield 
---------+-----+------------
 fiction |   3 | 2019-01
 sports  |   1 | 2019-01
 news    |   2 | 2019-01
 fiction |   3 | 2019-02
 sports  |   1 | 2019-02
 fiction |   5 | 2019-03
 news    |   6 | 2019-03

如果您能帮助我修复查询,我将不胜感激。

假设
代币每月至少有一个值,您可以执行以下操作:

select 
    c.name,
    sum(t.amount) sum_amount,
    to_char(m.monthfield, 'yyyy-mm') monthfield
from 
    category c
    cross join (select distinct date_trunc('month', datefield) monthfield from tokens) m
    left join tokens t
        on t.category = c.id
        and t.datefield >= m.monthfield
        and t.datefield < m.monthfield+ interval '1 month'
group by c.id, c.name, to_char(m.monthfield, 'yyyy-mm')
order by monthfield, c.id