postgresql - postgresql - 如何在聚合表和连接表输出中显示空值?
问题描述
我有2个表如下:
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
我已经编写了以下查询,但是我无法获得具有 NULL 值的行,如上面所需的输出所示:
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
如果您能帮助我解决查询,我将不胜感激。
解决方案
假设tokens
每月至少有一个值,您可以执行以下操作:
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
category
该查询在表和 中的所有可用月份之间生成笛卡尔积tokens
。然后,表格tokens
被带入left join
类别和日期范围。最后,数据按类别和月份汇总。
姓名 | 总金额 | 月场 :-------- | ---------: | :--------- 小说 | 3 | 2019-01 运动 | 1 | 2019-01 新闻 | 2 | 2019-01 小说 | 3 | 2019-02 运动 | 1 | 2019-02 新闻 | 空| 2019-02 小说 | 5 | 2019-03 运动 | 空| 2019-03 新闻 | 6 | 2019-03
推荐阅读
- python - 使用 Selenium 在下拉按钮中通过正则表达式选择值
- mysql - 如何获取与父表 id 相关的所有孙子
- flutter - 如何跳转到 PageView 中的特定页面?
- amazon-web-services - 使用 CodeDeploy 使用 AppSpec 或脚本设置权限
- css - CSS - 使所有图像都有一个圆形边框
- mysql - MySQL 基于 ID 和 Week 的分区
- ubuntu - 无法连接到 Travis 服务器上的 Mir
- python - 如何在python中的给定正方形中生成统一的新二维数据集
- objective-c - 不兼容的指针类型将“struct NSArray *”传递给“NSArray *”类型的参数
- windows - 在 Windows 上的 Docker 容器内运行 Kafka-Manager