表结构这样的,和上次一样:序号  f1  f2
1    1    a
2    1    b
3    1    c
4    2    a
5    2    b
6    3    a
7    3    b
8    3    c
9    3    d 
现在问题是要对 f1 和f2 都随机排序,并且排序出来f1 字段值一样的要分组在一起,比如这样的效果:
序号  f1  f2
4    2    a
5    2    b
1    1    a
2    1    b
3    1    c
6    3    a
7    3    b
8    3    c
9    3    d 
不知道能不能实现呢?我太菜了,想了很久没想出,希望大侠讲讲,谢谢!

解决方案 »

  1.   

    declare @t table(ID int,f1 int,f2 varchar(10))
    insert @t values(1,    1,    'a')
    insert @t values(2 ,   1 ,   'b')
    insert @t values(3  ,  1  ,  'c')
    insert @t values(4   , 2   , 'a')
    insert @t values(5,    2,    'b')
    insert @t values(6 ,   3 ,   'a')
    insert @t values(7  ,  3  ,  'b')
    insert @t values(8   , 3   , 'c')
    insert @t values(9    ,3    ,'d')select * from @t order by f1,newid()
    /*
    ID          f1          f2
    ----------- ----------- ----------
    1           1           a
    3           1           c
    2           1           b
    4           2           a
    5           2           b
    8           3           c
    9           3           d
    7           3           b
    6           3           a
    */
      

  2.   


    declare @t table(ID int,f1 int,f2 varchar(10))
    insert @t values(1,    1,    'a')
    insert @t values(2 ,   1 ,   'b')
    insert @t values(3  ,  1  ,  'c')
    insert @t values(4   , 2   , 'a')
    insert @t values(5,    2,    'b')
    insert @t values(6 ,   3 ,   'a')
    insert @t values(7  ,  3  ,  'b')
    insert @t values(8   , 3   , 'c')
    insert @t values(9    ,3    ,'d')select * from @t
    order by newid()
      

  3.   

    应该没有那样的结果。你既要按照F1,F2排序,又要按ID随机排序。做梦吧 = =
      

  4.   

    --sql2005:
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([序号] int,[f1] int,[f2] varchar(1))
    insert [tb]
    select 1,1,'a' union all
    select 2,1,'b' union all
    select 3,1,'c' union all
    select 4,2,'a' union all
    select 5,2,'b' union all
    select 6,3,'a' union all
    select 7,3,'b' union all
    select 8,3,'c' union all
    select 9,3,'d'
    go
    --select * from [tb]select a.序号,a.f1,a.f2
    from tb a
    ,(select f1,rn=row_number() over(order by newid()) from tb group by f1) b
    where a.f1=b.f1
    order by b.rn,newid()
    /*
    序号          f1          f2
    ----------- ----------- ----
    4           2           a
    5           2           b
    2           1           b
    3           1           c
    1           1           a
    9           3           d
    7           3           b
    6           3           a
    8           3           c(9 行受影响)
    */
      

  5.   


    declare @t table(ID int,f1 int,f2 varchar(10))
    insert @t values(1,    1,    'a')
    insert @t values(2 ,   1 ,   'b')
    insert @t values(3  ,  1  ,  'c')
    insert @t values(4   , 2   , 'a')
    insert @t values(5,    2,    'b')
    insert @t values(6 ,   3 ,   'a')
    insert @t values(7  ,  3  ,  'b')
    insert @t values(8   , 3   , 'c')
    insert @t values(9    ,3    ,'d')select * from @t
    order by F1,NewID() desc这样试试!
      

  6.   


    declare @t table(ID int,f1 int,f2 varchar(10))
    insert @t values(1,    1,    'a')
    insert @t values(2 ,   1 ,   'b')
    insert @t values(3  ,  1  ,  'c')
    insert @t values(4   , 2   , 'a')
    insert @t values(5,    2,    'b')
    insert @t values(6 ,   3 ,   'a')
    insert @t values(7  ,  3  ,  'b')
    insert @t values(8   , 3   , 'c')
    insert @t values(9    ,3    ,'d')select * from @t 
    order by 
    cast((rand()*99+1) as numeric(19,6))%(f1+1)
    --没算过几率,不过确实都有可能
      

  7.   


    declare @t table(ID int,f1 int,f2 varchar(10))
    insert @t values(1,    1,    'a')
    insert @t values(2 ,   1 ,   'b')
    insert @t values(3  ,  1  ,  'c')
    insert @t values(4   , 2   , 'a')
    insert @t values(5,    2,    'b')
    insert @t values(6 ,   3 ,   'a')
    insert @t values(7  ,  3  ,  'b')
    insert @t values(8   , 3   , 'c')
    insert @t values(9    ,3    ,'d')select * from @t 
    order by 
    cast((rand()*99+1) as numeric(19,6))%(f1+10)这个几率就差不多了
      

  8.   


    -------------------------------------------> 测试时间:2009-07-27
    --> 我的淘宝:http://shop36766744.taobao.com/--------------------------------------------------if object_id('[l8r]') is not null drop table [l8r]
    create table [l8r]([序号] int,[f1] int,[f2] varchar(1))
    insert [l8r]
    select 1,1,'a' union all
    select 2,1,'b' union all
    select 3,1,'c' union all
    select 4,2,'a' union all
    select 5,2,'b' union all
    select 6,3,'a' union all
    select 7,3,'b' union all
    select 8,3,'c' union all
    select 9,3,'d'select * from l8rselect * from(
    select *,flag=rand() from l8r where f1=1   union all
    select *,rand() from l8r where f1=2   union all
    select *,rand() from l8r where f1=3 )t
    order by flag,newid()/*
    序号          f1          f2   flag                                                  
    ----------- ----------- ---- ----------------------------------------------------- 
    6           3           a    0.3455370372278721
    9           3           d    0.3455370372278721
    8           3           c    0.3455370372278721
    7           3           b    0.3455370372278721
    2           1           b    0.38022202824029583
    3           1           c    0.38022202824029583
    1           1           a    0.38022202824029583
    5           2           b    0.6187124653788858
    4           2           a    0.6187124653788858(所影响的行数为 9 行)
    */drop table l8r
      

  9.   

    select 序号,f1,f2 from(
    select *,flag=rand() from l8r where f1=1   union all
    select *,rand() from l8r where f1=2   union all
    select *,rand() from l8r where f1=3 )t
    order by flag,newid()