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
如果没有组,它可以正常工作,没有错误。
答案 0 :(得分:0)
当您执行“分组依据”时,您会按一列或多列展平行。对于不在“group by”中的列,您需要告诉Postgres它应该如何使用聚合函数合并列的数据。
http://www.postgresql.org/docs/current/static/functions-aggregate.html
在你的情况下,当有多个行具有相同的application_id时,你需要告诉Postgres如何展平列attribute_id和attribute_value。它应该返回max()id和value吗?应该把它们加在一起吗?它应该将它们连接成一组值吗?它应该返回平均值吗?
select app_id, max(attr_id) as max_attr_id from foo group by app_id;