怎么每五行给行号,比如
name   行号
a        1
b        2
c        3
d        4
e        5
f        1
g        2
h        3
i        4
j        5

解决方案 »

  1.   

    select name, (IDENTITY(1,1)%5 +1  from TB
      

  2.   


    declare @t table ( c char(1))
    insert into @t
    select 'a' union all
    select 'b' union all
    select 'c' union all
    select 'd' union all
    select 'e' union all
    select 'f' union all
    select 'g' union all
    select 'h' union all
    select 'i' union all
    select 'j' select case when r%5 = 0 then 5 else r%5 end as Xh,* from (
    select row_number()over(order by getdate()) as R, * from @t
    ) t/*
    (10 行受影响)
    Xh                   R                    c
    -------------------- -------------------- ----
    1                    1                    a
    2                    2                    b
    3                    3                    c
    4                    4                    d
    5                    5                    e
    1                    6                    f
    2                    7                    g
    3                    8                    h
    4                    9                    i
    5                    10                   j(10 行受影响)
    */