【发布时间】:2022-01-21 13:01:05
【问题描述】:
我有以下表格
CREATE TABLE categories(
id SERIAL,
);
CREATE TABLE category_translations(
id SERIAL,
name varchar not null,
locale varchar not null,
category_id integer not null
);
CREATE TABLE products(
id SERIAL,
category_id integer not null
);
CREATE TABLE line_items(
id SERIAL,
total_cents integer
product_id integer not null
);
我要做的是将每个类别名称的映射输出到与其关联的line_itemstotal_cents 的总和。比如:
| name | sum_total_cents |
|---|---|
| Fresh foods | 100000 |
| Dry products | 532000 |
有一个唯一性约束,即每个语言环境只存储一个名称。因此,category 将在 category_translations 表中存储的每个区域设置一行
我目前拥有的是
SELECT SUM(line_items.total_cents) AS sum_total_cents, ???
FROM line_items INNER JOIN products ON products.id = line_items.product_id
INNER JOIN categories ON categories.id = products.category_id
LEFT OUTER JOIN category_translations ON category_translations.category_id = categories.id
WHERE category_translations.locale ='en'
GROUP BY categories.id
我正在寻找一个聚合函数来返回该类别的第一个 name。唯一缺少的是要写什么而不是???,因为我遇到了很多must appear in the GROUP BY clause or be used in an aggregate function 错误。在伪代码中,我正在寻找可以使用的 PostgreSQL 中的 FIRST() 聚合方法
【问题讨论】:
-
避免使用旧的
SERIAL,改用IDENTITY。 -
"...类别的名字..." -- 没有这样的东西。你的意思是随机的名字吗?请记住,关系数据库中的行没有固有的顺序。
-
@TheImpaler 我有一个唯一性约束,即每个语言环境只有一个名称将存储在表中。因此,
category将在category_translations表中存储的每个区域设置一行 -
这完全没问题,但每个类别仍然有多个名称。你想选哪一个?所有这些多个名称都没有顺序,所以没有“第一个”。
-
如果您有
WHERE category_translations.locale ='en',您实际上已将LEFT JOIN更改为INNER JOIN。您可以将MAX(category_translations.name) AS name添加到选择列表中以避免错误。该错误只是指功能依赖要求。不需要子查询。
标签: sql postgresql left-join aggregate-functions