Pergunta

Is it possible to populate a column in SQL that looks like this? Original column on the left, the one I want on the right.

A...1
B...1
C...1
D...1
A...2
C...2
E...1
A...3
D...2

i.e. the first occurrence of each letter is 1, the second occurrence of each letter (going down) is a 2, the third occurrence of each letter is a 3 etc.

Foi útil?

Solução

  SELECT col1,
  ROW_NUMBER( ) OVER (PARTITION BY
  col1 ORDER BY col1
  NULLS LAST) SRLNO
  FROM table;

Replace col1 with 'column name'

and

table with 'table name'

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top