select column_1, column_2, column_3 from test_table; 

A query that returns a table with 3 columns.

 aaa 11 123 bbb 12 232 ccc 14 222 aaa 16 222 sss 19 333 bbb 19 331 aaa 20 999 

It is necessary to display this table as follows.

 aaa 11 123 16 222 20 999 bbb 12 232 19 331 ccc 14 222 sss 19 333 

How can this be done ?. I guess it needs to be done through order by but somehow you need to get intermediate results.

    1 answer 1

    We use window functions and subquery:

     select case when rnk=1 then column1 else '' end column1, column2, column3 from ( select row_number() over (partition by column1 order by column3) rnk, column1, column2, column3 from test_table) t order by t.column1, rnk 

    Fiddle

    • And if you need to do so with multiple columns. In our example, not only the first column of this type of lead, but the second too. - Lucifer
    • Add a second window function with the partch in the second column and in the condition of displaying the second column you register its result - Zufir