postgresql中的SQL按子查询分组
我想按愚弄表分组,但我还需要将列ID分组为图像上的depitted 有什么想法吗postgresql中的SQL按子查询分组,sql,postgresql,Sql,Postgresql,我想按愚弄表分组,但我还需要将列ID分组为图像上的depitted 有什么想法吗 致以最诚挚的问候在标准SQL中,这看起来像: select name, sku, ml, '[' || listagg(id, ',') within group (order by id) || ']' as ids from t group by name, sku, ml order by name, count(*); 但是,并非所有数据库都支持用于字符串连接的标准运算符|。并非所有数据
致以最诚挚的问候在标准SQL中,这看起来像:
select name, sku, ml,
'[' || listagg(id, ',') within group (order by id) || ']' as ids
from t
group by name, sku, ml
order by name, count(*);
但是,并非所有数据库都支持用于字符串连接的标准运算符|
。并非所有数据库都调用其字符串聚合运算符listag()
。因此,您可能需要调整数据库的查询
编辑:
在博士后,这将是:
select name, sku, ml,
'[' || string_agg(id, ',' order by id) || ']' as ids
from t
group by name, sku, ml
order by name, count(*);
看起来您需要一个JSON数组。这可以使用
jsonb_agg()
完成:
用您正在使用的数据库标记您的问题。请不要将代码作为图像发布。请参阅此处了解更多详细信息为什么:好,完成。谢谢,我正在使用postgresql。谢谢
select name, sku, ml,
'[' || string_agg(id, ',' order by id) || ']' as ids
from t
group by name, sku, ml
order by name, count(*);
select name, sku, ml, jsonb_agg(id)
from the_table
group by name, sku, ml;