Skip to content
Advertisement

Is there any way in Oracle to skip a value in a select when it has been already shown?

Sorry because maybe it’s a silly question but honestly I don’t know how to handle it. Let’s imagine that after executing a select with a group by clause I have the following information:

col1    col2    col3
---------------------
value1  optionA  1
value1  optionB  5
value2  optionA  3
value2  optionB  4

But I would like to obtain the following:

col1    col2    col3
---------------------
value1  optionA  1
        optionB  5
value2  optionA  3
        optionB  4

In other words, I don’t want to show a previously shown value if it’s the same (in this case, value1 and value2)

Is this feasible with Oracle? if the answer if yes, would you kindly point me in the right direction?

Thank you.

Advertisement

Answer

You don’t say what’s the ordering criteria for the rows so I’ll assume it’s (col1, col2).

The query you want should look like:

select
  case when col1 = lag(col1) over(order by col1, col2) 
       then null
       else col1
  end as col1,
  col2, 
  col3
from t
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement