我有一个如下所示的查询:
SELECT *,
(SELECT json_agg(deals.*) FROM deals WHERE vendors.id = deals.vendorid ORDER BY priority) as deals
FROM vendors
该查询效果很好,但是当我尝试通过error: column "deals.priority" must appear in the GROUP BY clause or be used in an aggregate function
表中的字段进行排序时,这样:
{{1}}
我收到以下错误:
{{1}}
任何想法如何做这样的事情?
答案 0 :(得分:1)
语法不应该是这个吗?
(SELECT json_agg(deals.*) WITHIN GROUP (ORDER BY priority)
FROM deals
WHERE vendors.id = deals.vendorid
) as deals
文档为here。
答案 1 :(得分:1)
你可以
SELECT *,
(SELECT json_agg(deals.* ORDER BY priority) FROM deals WHERE vendors.id = deals.vendorid) as deals
FROM vendors