Increment Column

  • I want to show all registers from a table(CODE, DESCRIPTION) and show an additional collum with the row number(INCREMENT).

    Select * from table_1

    RESULT:

    CODE     DESCTIPTION               INCREMENT

    -------- ------------------------- -----------

    03354718 FIRST_DESCRIPTION         1

    54654654 SECOND_DESCRIPTION      2

    62173620 THIRD_DESCRIPTION         3

    How can I get it?

    Thank you very much

  • set nocount on

    declare @t table(CODE varchar(50),DESCRIPTION varchar(50))

    insert into @t values ('03354718', 'FIRST_DESCRIPTION')

    insert into @t values ('54654654','SECOND_DESCRIPTION')

    insert into @t values ('62173620','THIRD_DESCRIPTION')

    select * From @t

    select a.code,a.description,count(*)

    from @t a join @t b

    on a.code>=b.code

    group by a.code,a.description


    Kindest Regards,

    Vasc

  • DECLARE @Table_1 TABLE

    (

    CODE  VARCHAR(10),

    [DESCRIPTION] VARCHAR(100)

    )

    INSERT INTO @Table_1 VALUES('03354718', 'FIRST_DESCRIPTION')

    INSERT INTO @Table_1 VALUES('54654654', 'SECOND_DESCRIPTION')

    INSERT INTO @Table_1 VALUES('62173620', 'THIRD_DESCRIPTION')

    INSERT INTO @Table_1 VALUES('62173622', 'FOURTH_DESCRIPTION')

    INSERT INTO @Table_1 VALUES('62173624', 'FIFTH_DESCRIPTION')

    INSERT INTO @Table_1 VALUES('62173626', 'SEVENTH_DESCRIPTION')

    SELECT A.CODE, MIN(A.[DESCRIPTION]) [DESCRIPTION], COUNT(*) INCREMENT

    FROM

     @Table_1 A

    JOIN

     @Table_1 B

    ON

     A.[DESCRIPTION] >= B.[DESCRIPTION]

    GROUP BY A.CODE

    ORDER BY COUNT(*)

    HAH Vasc You beat me (also I should have used CODE for comparison)

    Regards,
    gova

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

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