aa(a,b,c)with m as(select top(100)Percent c,a,row_number() over(order by NEWID() desc) as n from bb)
select * from bb as xx where xx.a = (select top 1 m.a from m where m.c= xx.c  order by m.n)按C分组 a是主键求更好的解法

解决方案 »

  1.   


    with m as
    (select top(100)Percent c,a,row_number() over(partition by c order by NEWID() desc) as n from bb)
    select * from bb as xx where xx.a = 1
    试试
      

  2.   

    with m as
    (select top(100)Percent c,a,
    row_number() over(partition by c order by NEWID()) as n 
    from bb)
    select * from m as xx where xx.n = 1改改,那个看错了
      

  3.   

    --> 测试数据: [aa]
    if object_id('[aa]') is not null drop table [aa]
    create table [aa] (a int,b int,c int)
    insert into [aa]
    select 1,2,3 union all
    select 1,3,4 union all
    select 1,4,5 union all
    select 2,12,3 union all
    select 2,14,4 union all
    select 2,13,5select * from [aa] t where b =(select top 1 b from aa where a=t.a order by newid())
      

  4.   

    SELECT * FROM AA T WHERE A=(SELECT TOP 1 A FROM AA WHERE C=T.C ORDER BY NEWID())