Oracle: Conditional Grouping -


i having data in following format :

 cons_type   column_name     p        (col1)     r        (col6_reference)     r        (col6_reference)     u        (col5_com_unique)     u        (col3_unique,col4_com_unique) 

finally, want listagg column_name, cons_type wise cons_type either 'p' or 'u' only.

other cons_type 'r' must not list aggregated using listagg() function.

and final expected output must in following format.

cons_type   column_name p            (col1) r            (col6_reference) r            (col6_reference) u            (col3_unique,col4_com_unique),(col5_com_unique) 

try:

select      "cons_type",      "column_name" tbl "cons_type" not in ('p', 'u')  union  select      "cons_type",      listagg("column_name" , ',') within group (order "cons_type") tbl "cons_type" in ('p', 'u') group "cons_type"  order "cons_type" 

demo sqlfiddle


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -