在SQL中如何写SQL子查询?
How to write SQL sub-query in SQL?
这是我正在寻找基于国家/地区的总买入交易价值和总卖出交易价值的样本数据。
这里有两个表,国家和行业
Table [公司]:
+-------------+--------------------+
| name| country |
+-------------+--------------------+
| Alice s.p. | Wonderland |
| Y-zap | Wonderland |
| Absolute | Mathlands |
| Arcus t.g. | Mathlands |
| Lil Mermaid | Underwater Kingdom |
| None at all | Nothingland |
+-------------+--------------------+
Table [交易]:
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
尝试 CTE:
WITH sold AS (
SELECT sum(t.value) AS value, c.country FROM trades AS t INNER JOIN companies AS c ON (t.seller = c.name) GROUP BY c.country
), buyed AS (
SELECT sum(t.value) AS value, c.country FROM trades AS t INNER JOIN companies AS c ON (t.buyer = c.name) GROUP BY c.country
)
SELECT DISTINCT c.country, COALESCE(b.value, 0) AS buyer, COALESCE(s.value, 0) AS seller
FROM companies AS c
LEFT JOIN sold AS s ON (c.country = s.country)
LEFT JOIN buyed AS b ON (c.country = b.country)
将 country
加入 trades
的不同行,其中仅包含买家或卖家并有条件地聚合:
SELECT c.country,
SUM(CASE WHEN buyer IS NOT NULL THEN value ELSE 0 END) buyer,
SUM(CASE WHEN seller IS NOT NULL THEN value ELSE 0 END) seller
FROM country c
LEFT JOIN (
SELECT buyer, null seller, value FROM trades
UNION ALL
SELECT null, seller, value FROM trades
) t ON c.name IN (t.buyer, t.seller)
GROUP BY c.country
或者,使用 SUM() window 函数:
SELECT DISTINCT c.country,
SUM(CASE WHEN c.name = t.buyer THEN value ELSE 0 END) OVER (PARTITION BY c.country) buyer,
SUM(CASE WHEN c.name = t.seller THEN value ELSE 0 END) OVER (PARTITION BY c.country) seller
FROM country c LEFT JOIN trades t
ON c.name IN (t.buyer, t.seller)
参见demo。
这是我正在寻找基于国家/地区的总买入交易价值和总卖出交易价值的样本数据。
这里有两个表,国家和行业 Table [公司]:
+-------------+--------------------+
| name| country |
+-------------+--------------------+
| Alice s.p. | Wonderland |
| Y-zap | Wonderland |
| Absolute | Mathlands |
| Arcus t.g. | Mathlands |
| Lil Mermaid | Underwater Kingdom |
| None at all | Nothingland |
+-------------+--------------------+
Table [交易]:
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
尝试 CTE:
WITH sold AS (
SELECT sum(t.value) AS value, c.country FROM trades AS t INNER JOIN companies AS c ON (t.seller = c.name) GROUP BY c.country
), buyed AS (
SELECT sum(t.value) AS value, c.country FROM trades AS t INNER JOIN companies AS c ON (t.buyer = c.name) GROUP BY c.country
)
SELECT DISTINCT c.country, COALESCE(b.value, 0) AS buyer, COALESCE(s.value, 0) AS seller
FROM companies AS c
LEFT JOIN sold AS s ON (c.country = s.country)
LEFT JOIN buyed AS b ON (c.country = b.country)
将 country
加入 trades
的不同行,其中仅包含买家或卖家并有条件地聚合:
SELECT c.country,
SUM(CASE WHEN buyer IS NOT NULL THEN value ELSE 0 END) buyer,
SUM(CASE WHEN seller IS NOT NULL THEN value ELSE 0 END) seller
FROM country c
LEFT JOIN (
SELECT buyer, null seller, value FROM trades
UNION ALL
SELECT null, seller, value FROM trades
) t ON c.name IN (t.buyer, t.seller)
GROUP BY c.country
或者,使用 SUM() window 函数:
SELECT DISTINCT c.country,
SUM(CASE WHEN c.name = t.buyer THEN value ELSE 0 END) OVER (PARTITION BY c.country) buyer,
SUM(CASE WHEN c.name = t.seller THEN value ELSE 0 END) OVER (PARTITION BY c.country) seller
FROM country c LEFT JOIN trades t
ON c.name IN (t.buyer, t.seller)
参见demo。