数据有很多条,每隔10个记录取10个记录,该如何写语句?

解决方案 »

  1.   


    --数据有很多条,每隔10个记录取10个记录,该如何写语句?
    if object_id('tb') is not null drop table tb
    go
    create table tb(id int identity(1,1),number int)
    go
    declare @id int
    set @id=1
    while (@id<100)
    begin 
        insert into tb values(@id)
        set @id=@id+1
    end
    --
    select  * from tb where left(id,1) in(2,4,6,8) and len(id)>1
    --//结果
    id          number
    ----------- -----------
    20          20
    21          21
    22          22
    23          23
    24          24
    25          25
    26          26
    27          27
    28          28
    29          29
    40          40
    41          41
    42          42
    43          43
    44          44
    45          45
    46          46
    47          47
    48          48
    49          49
    60          60
    61          61
    62          62
    63          63
    64          64
    65          65
    66          66
    67          67
    68          68
    69          69
    80          80
    81          81
    82          82
    83          83
    84          84
    85          85
    86          86
    87          87
    88          88
    89          89(40 行受影响)
      

  2.   

    select *
    from (
    select *,row_number() over (order by getdate()) rn
    from tb
    ) t
    where ((rn-1)/2)%2=0
      

  3.   

    select *
    from (
    select *,row_number() over (order by getdate()) rn
    from tb
    ) t
    where ((rn-1)/10)%2=0