标签出现次数

时间:2015-09-17 17:58:11

标签: sql

我的数据类似于:

    a | "Random String"
    b | "Random String"
    b | "Random String"
    c | "Random String"
    d | "Random String"
    d | "Random String"
    d | "Random String"
    a | "Random String"

我想根据字母列的出现次数标记数据:

    1 | a | "Random String"
    1 | b | "Random String"
    2 | b | "Random String"
    1 | c | "Random String"
    1 | d | "Random String"
    2 | d | "Random String"
    3 | d | "Random String"
    2 | a | "Random String"

2 个答案:

答案 0 :(得分:0)

使用大多数数据库支持的ANSI标准row_number()

select row_number() over (partition by col1 order by col1), col1, col2

答案 1 :(得分:0)

没有row_number():

SELECT 
(SELECT 1+COUNT(letter) 
    FROM TableName WHERE id < tn.id AND letter = tn.letter) AS occurrences,    
tn.letter,
tn.random_string
FROM TableName tn