首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在postgresql中同时使用group by和order by

如何在postgresql中同时使用group by和order by
EN

Stack Overflow用户
提问于 2014-05-10 13:31:30
回答 1查看 7.5K关注 0票数 2
代码语言:javascript
运行
复制
select t.spdi_application_id,t.spdi_attribute_id, t.spdi_attribute_value 
from schm_sp.spe_service_appl_cert_details t 
where spdi_attribute_id in(395,263,397,396,75) 
GROUP BY spdi_application_id 
ORDER BY spdi_application_id,spdi_attribute_id 

我正在使用此命令,但收到如下错误

代码语言:javascript
运行
复制
ERROR:  column "t.spdi_attribute_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select t.spdi_application_id,t.spdi_attribute_id, t.spdi_att...
                                     ^

********** Error **********

ERROR: column "t.spdi_attribute_id" must appear in the GROUP BY clause or be used in an aggregate function
SQL state: 42803
Character: 30

如果没有group by,它工作得很好,没有错误。

EN

回答 1

Stack Overflow用户

发布于 2014-05-10 13:51:19

当您执行"group by“时,您是在按一列或多列来展平行。对于不在"group by“中的列,您需要告诉Postgres应该如何使用聚合函数合并列数据。

http://www.postgresql.org/docs/current/static/functions-aggregate.html

在您的示例中,您需要告诉Postgres,当有多个行具有相同的application_id时,如何展平列attribute_id和attribute_value。它应该返回max() id和值吗?它应该将它们相加()吗?它是否应该将它们连接到一个值数组中?它应该返回平均值吗?

代码语言:javascript
运行
复制
select app_id, max(attr_id) as max_attr_id from foo group by app_id;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23577460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档