Row_Number simulation in Sql server 2000

后端 未结 2 693
心在旅途
心在旅途 2020-12-11 12:42

I have a sample input table as

Declare  @input TABLE(Name VARCHAR(8))
INSERT INTO @input(Name)  values(\'Aryan\') 
INSERT INTO @input(Name)  values(\'Aryan\         


        
相关标签:
2条回答
  • 2020-12-11 13:26

    Declare your table variable as

    Declare  @input TABLE(_id int identity(1, 1), Name VARCHAR(8))
    

    And then reqrite your query as

    Select _id, name 
    from @input
    where _id % 2 <> 0
    
    0 讨论(0)
  • 2020-12-11 13:28

    Use this query:

    SELECT t1.name, t.n
    FROM
    (
        SELECT a.name, a.c, (SELECT COUNT(*) FROM @input AS i2 WHERE i2.Name <= a.Name) [rn]
        FROM
        (
            SELECT i.name, count(*) c
            FROM @input i
            GROUP BY i.name
        )a
    )t1
    JOIN @t t ON t.n <= t1.rn
    WHERE t.n > t1.rn - t1.c 
    

    It produces desired output:

    name     n
    -------- -----------
    Aryan    1
    Aryan    2
    Aryan    3
    Aryan    4
    Jaesmin  5
    Jaesmin  6
    Jaesmin  7
    Joseph   8
    Joseph   9
    Marya    10
    Padukon  11
    Padukon  12
    Vick     13
    Vicky    14
    Vicky    15
    Vicky    16
    
    0 讨论(0)
提交回复
热议问题