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 我正在使用此命令,但收到如下错误
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,它工作得很好,没有错误。
发布于 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和值吗?它应该将它们相加()吗?它是否应该将它们连接到一个值数组中?它应该返回平均值吗?
select app_id, max(attr_id) as max_attr_id from foo group by app_id;https://stackoverflow.com/questions/23577460
复制相似问题