Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

oracle array : 1. aggregate + window function 2. collation according to datatype

Jim DicksonFeb 15 2025 — edited Feb 15 2025

I am trying to write code equivalent to below in Oracle 23

The keypoints are aggregation of array ( window functions ) and collation according to datatype.

Code below runs in Postgres 17 and DuckDB 1.1.x

with v01 as ( select x.partkey, x.val1::integer as val1, x.val2 
from ( values ('partkey1', 7, 'g'), 
('partkey1', 10, 'j'), 
('partkey2', 7, 'g'), 
('partkey2', 2, 'b')) 
as x(partkey, val1, val2) ) , 
v02 as ( select v.* 
, array_agg(v.val1) over (win_v02) as integer_array 
, string_agg(v.val1::varchar, ',') over (win_v02) as varchar_array -- for collation comparison & visualisation 
from v01 v 
window win_v02 as (partition by v.partkey) ), 
v03 as ( select v.* 
, rank() over (order by v.integer_array, v.val2) as integer_array_rank -- orders by val1::integer 
, rank() over (order by v.varchar_array, v.val2) as varchar_array_rank -- orders by val1::varchar 
from v02 v ) 
select v.val1, v.val2, v.integer_array, v.integer_array_rank, v.varchar_array, v.varchar_array_rank from v03 v 
order by v.integer_array_rank ;

This post has been answered by Jim Dickson on Feb 28 2025
Jump to Answer
Comments
Post Details
Added on Feb 15 2025
30 comments
567 views