Question

I have the following table in input:

FootnoteField               FootnoteText
Field1                      blabla
Field2                      blabla 
Field3                      blabla 
Field4                      zzzzzzzzzz 
Field5                      blablabla 
Field6                      ploof 

How could I add an incremental integer column, that assigns the same integer for rows where FootnoteText is identical? This is what I am looking to achieve here:

FootnoteField                   FootnoteText       Number
Field1                          blabla               1
Field2                          blabla               1
Field3                          blabla               1
Field4                          zzzzzzzzzz           2
Field5                          blablabla            3
Field6                          ploof                4
Was it helpful?

Solution

If you don't need your data ordered by FootnoteField, you can go with GoatCo answer.

But if you do then:

with cte as (
    select
        FootnoteText, FootnoteField,
        min([FootnoteField]) over(partition by FootnoteText) as min_FootnoteText
    from Table1
)
select 
    FootnoteText, FootnoteField,
    dense_rank() over(order by min_FootnoteText)
from cte

sql fiddle demo

OTHER TIPS

You probably want DENSE_RANK():

SELECT *, DENSE_RANK() OVER(ORDER BY FootnoteText)
FROM YourTable

You only need PARTITION BY if you want the numbering to start over for each value in some group, ie for each day I want numbering to start at 0, so PARTITION BY Some_Date

Demo: SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top