使用sum在postgresql中使用sum时从选择子句中删除列

发布于 2025-01-23 05:30:15 字数 1811 浏览 0 评论 0原文

我检查了在这里< /a>,但没有解决我的问题,因为我什至添加else仍然丢弃相同的错误:

ERROR:  column table1.column11 must appear in the GROUP BY clause or be used in an aggregate function

我想从中删除table1.column11 select> select> select /code>是因为会影响组。而不是每个enum键入单行中的列,下面的查询是返回enum列以不同行填充的表。

查询示例:

SELECT table1.column11, -- enum column
   table2.id,
   (case when table1.column11 = 'enum_value_1' then SUM(table1.column12) end) as enum_value_1,
   (case when table1.column11 = 'enum_value_2' then SUM(table1.column12) end) as enum_value_2,
   --- ...
   (case when table1.column11 = 'enum_value_9' then SUM(table1.column19) end) as enum_value_9,
FROM table1
LEFT JOIN table2 ON table2.id = table1.table2_id
WHERE table1.column13 IS NULL
GROUP BY table2.id, table1.column11
ORDER BY table2.id ASC

现在如何:

table1.column11 | table2.id | enum_value_1 | enum_value_2 | ... | enum_value_9
================|===========|==============|==============|=====|=============
enum_value_1    | 1         | 10           | [null]       | ... | [null]
enum_value_2    | 1         | [null]       | 20           | ... | [null]
...             | ...       | ...          | ...          | ... | ...
enum_value_9    | 1         | [null]       | [null]       | ... | 15

我想如何:

table2.id | enum_value_1 | enum_value_2 | ... | enum_value_9
==========|==============|==============|=====|=============
1         | 10           | 20           | ... | 15

I checked the answer here but didn't solve my problem because I even adding the else still throwing the same error:

ERROR:  column table1.column11 must appear in the GROUP BY clause or be used in an aggregate function

I would like to remove table1.column11 from SELECT because is affecting the GROUP BY. Instead of each ENUM type be a column in a single line this query below is returning a table with a ENUM column filled in different lines.

Query example:

SELECT table1.column11, -- enum column
   table2.id,
   (case when table1.column11 = 'enum_value_1' then SUM(table1.column12) end) as enum_value_1,
   (case when table1.column11 = 'enum_value_2' then SUM(table1.column12) end) as enum_value_2,
   --- ...
   (case when table1.column11 = 'enum_value_9' then SUM(table1.column19) end) as enum_value_9,
FROM table1
LEFT JOIN table2 ON table2.id = table1.table2_id
WHERE table1.column13 IS NULL
GROUP BY table2.id, table1.column11
ORDER BY table2.id ASC

How is now:

table1.column11 | table2.id | enum_value_1 | enum_value_2 | ... | enum_value_9
================|===========|==============|==============|=====|=============
enum_value_1    | 1         | 10           | [null]       | ... | [null]
enum_value_2    | 1         | [null]       | 20           | ... | [null]
...             | ...       | ...          | ...          | ... | ...
enum_value_9    | 1         | [null]       | [null]       | ... | 15

How I would like to have:

table2.id | enum_value_1 | enum_value_2 | ... | enum_value_9
==========|==============|==============|=====|=============
1         | 10           | 20           | ... | 15

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2025-01-30 05:30:16

问题中的例子很难复制,但我认为我的建议将有所帮助。从选择列表中删除table1.column11,并仅在聚合中使用列。

因此,代替:

SELECT table1.column11, -- enum column
   table2.id,
   (case when table1.column11 = 'enum_value_1' then SUM(table1.column12) end) as enum_value_1,

使用:

SELECT
   table2.id,
   sum(case when table1.column11 = 'enum_value_1' then table1.column12 end) as enum_value_1,

The example in the question is difficult to reproduce, but I think my suggestions will be helpful. Remove table1.column11 from the select list and use the column only inside the aggregates.

So instead of:

SELECT table1.column11, -- enum column
   table2.id,
   (case when table1.column11 = 'enum_value_1' then SUM(table1.column12) end) as enum_value_1,

use:

SELECT
   table2.id,
   sum(case when table1.column11 = 'enum_value_1' then table1.column12 end) as enum_value_1,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文