Query!!

  • Hi

    my col1 has below data

    col1

    abc

    abc

    abc

    fgh

    dft

    fgh

    I have a requirement to update col2 in same table where col2 should contain values like

    col1 col2

    abc 1

    abc 2

    abc 3

    fgh 1

    dft 1

    fgh 2

    Can you please help me write this query

  • You will need to use the row_number() function.

    BOL has several good examples



    Clear Sky SQL
    My Blog[/url]

  • --Creagin the table and inserting the data

    create table #t (string char(3))

    insert into #t (string)

    select 'abc'

    union all select 'abc'

    union all select 'abc'

    union all select 'fgh'

    union all select 'dft'

    union all select 'fgh'

    go

    --Using row_number() function with partition by and order by clauses

    select string, row_number() over (partition by string order by string) as RowNum

    from #t

    go

    --Cleanup

    drop table #t

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply