Andy Vincent
Andy Vincent

Reputation: 83

How to select (i.e. second) a group in SQL (Oracle)

my column look like this:

delta
delta
alpha
alpha
alpha
gamma
gamma
gamma

I'd like to group it and order. So it will result with:

1. alpha
2. delta
3. gamma

And that's still easy. Now, the point is to tell SQL "Show me the second group", not knowing what data we will actually have there. In other words - not naming the group, just by a number. Any idea?

Upvotes: 2

Views: 83

Answers (1)

sstan
sstan

Reputation: 36483

You can use the row_number() analytic function for that:

select your_col
  from (select your_col,
               row_number() over (order by your_col) as rn
          from your_table
         group by your_col)
 where rn = 2

Upvotes: 1

Related Questions