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.

有帮助吗?

解决方案

  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'

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top