使用sum在postgresql中使用sum时从选择子句中删除列
我检查了在这里< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题中的例子很难复制,但我认为我的建议将有所帮助。从选择列表中删除
table1.column11
,并仅在聚合中使用列。因此,代替:
使用:
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:
use: