表:
ID
1
1
1
1
1
2
2
2
2
2
3
3
3
3我需要读取3个1,3个2,3个3.......等等,也就是说每个唯一id读取3条,怎么写?

解决方案 »

  1.   

    又是随机选题的,循环产生这样的sql:
    select *  from  (select top 3 *,newid() as a  from yourtable where id=1 order by newid()) b
    union  
    select *  from  (select top 3 *,newid() as a  from yourtable where id=2 order by newid() ) b
    ...
    order by a
      

  2.   

    如果是2005,看看title,rank等分组语句的用法,应该能用一条语句解决的
      

  3.   

    希望能够一句搞定,谢谢amendajing
    如果是2005,看看title,rank等分组语句的用法,应该能用一条语句解决的
    ???????什么意思?
      

  4.   

    create table tb(id int)
    insert into tb select 1
    union all select 1
    union all select 1
    union all select 1
    union all select 1
    union all select 2
    union all select 2
    union all select 2
    union all select 2
    union all select 2
    union all select 2
    union all select 2
    union all select 3
    union all select 3
    union all select 4
    union all select 4declare @sql varchar(8000)
    set @sql='select '
    select @sql=@sql+' top 3 * from tb where id ='+rtrim(a.id)+' union all select'  from (select distinct id from tb) a
    select @sql=left(@sql,len(@sql)-17)
    --print @sql
    exec(@sql)