【发布时间】:2021-07-19 09:36:33
【问题描述】:
这是我正在寻找基于国家/地区的总买入交易价值和总卖出交易价值的示例数据。
这里有两张表,国家和行业 表[公司]:
+-------------+--------------------+
| name| country |
+-------------+--------------------+
| Alice s.p. | Wonderland |
| Y-zap | Wonderland |
| Absolute | Mathlands |
| Arcus t.g. | Mathlands |
| Lil Mermaid | Underwater Kingdom |
| None at all | Nothingland |
+-------------+--------------------+
表[交易]:
trades:
+----------+-------------+------------+-------+
| id | seller | buyer | value |
+----------+-------------+------------+-------+
| 20121107 | Lil Mermaid | Alice s.p. | 10 |
| 20123112 | Arcus t.g. | Y-zap | 30 |
| 20120125 | Alice s.p. | Arcus t.g. | 100 |
| 20120216 | Lil Mermaid | Absolute | 30 |
| 20120217 | Lil Mermaid | Absolute | 50 |
+----------+-------------+------------+-------+
预期输出:
+--------------------+--------+--------+
| country| buyer | seller|
+--------------------+--------+--------+
| Mathlands | 180 | 30 |
| Nothingland | 0 | 0 |
| Underwater Kingdom | 0 | 90 |
| Wonderland | 40 | 100 |
+--------------------+--------+--------+
我正在尝试这个:它只给出一个值列,并且它没有显示我想要显示的 0 贸易国家。
select country, sum(value), sum(value)
from
(select a.buyer as export, a.seller as import, value, b.country as country
from trades as a
join companies as b
on a.seller=b.name)
group by country
order by country
请帮忙
【问题讨论】:
-
如果您使用的是 [postgresql] 为什么要在所有其他 RDBMS 中标记垃圾邮件..?不要标记垃圾邮件;它不能帮助我们帮助你。它不会给你的问题更多的观点。这只会让人感到困惑,因为我们不知道您真正使用的是什么技术,这意味着该问题无法回答,没有用处,因此可能会导致投票失败
-
感谢我将其更改为 SQLite,其中查询运行良好但仍然缺少一些输出详细信息
-
“我把它改成 SQLite” 标签上写着 [postgresql] not [sqlite]...