将oracle的listagg转换为postgres

时间:2018-05-26 03:53:04

标签: sql oracle postgresql

我正在尝试将一些Oracle查询转换为Postgres并遇到listagg

Oracle代码

select max(eta) eta, mso_id, listagg(carrier_name, ',') 
within group (order by eta) as carrier 
from tb_flight_schedule group by mso_id;

我发现Postgres相当于listaggstring_agg,并继续将listaggstring_agg交换。但是,我遇到错误(错误:函数string_agg(字符变化,未知,日期)不存在)。我在查询中遗漏了什么吗? Postgres查询如下:

select max(eta) eta, mso_id, string_agg(carrier_name, ',') 
WITHIN GROUP (ORDER BY eta) as carrier 
from tb_flight_schedule group by mso_id;

1 个答案:

答案 0 :(得分:4)

在Postgres中,within group子句仅用于有序集合函数 - 即需要 order by子句的函数(例如,百分位函数)。可选的order by子句,例如string_agg,位于函数的参数内:

SELECT   MAX(eta) eta, mso_id, STRING_AGG(carrier_name, ',' ORDER BY eta) AS carrier 
FROM     tb_flight_schedule
GROUP BY mso_id;