字段1 字段2
id  name
1   a
2   b
3   c
4   c
5   b
根据name分组,最后每组随机取出一条数据。网上有许多类似的代码,但是运行有问题啊MS SQL分组后随机取出数据

解决方案 »

  1.   

    思路可以有1、将内容分组,如:第一组name=a,b,c,d,第二组name=e,f,g,h
    2、1—4的范围内获得一个随机数,赋值给checki
    3、split(name,",")(checki)
    4、如果内容分组后,第一组和第二组的数量不一致——"a,b,c,d"和"e,f,g",用下标对比处理一下checki
      

  2.   

    select id,name from(select *,row=row_number()over(partition by name order by newid()) from tb)t where row=1
      

  3.   

    服务器的版本貌似是SQL2000,于是乎:'row_number' 不是可以识别的 函数名。
      

  4.   

    create table t
    (
    id int,
    name varchar(10)
    )
    insert into t
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' select t.name,MAX(id)
    from t
    group by t.namedrop table t
      

  5.   

    改一下6楼的代码,这样可以随机,但是在数据量大的时候才容易随机create table t 

    id int, 
    name varchar(10) 

    insert into t 
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' 
      
    select t.name,(SELECT TOP 1 id FROM t a WHERE a.NAME=t.NAME ORDER BY CHECKSUM(NEWID()))id
    from t 
    group by t.name
      
    drop table t
      

  6.   


    declare @tab table(id int,name char(1))
    insert into @tab
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c' union all
    select 4,'c' union all
    select 5,'b' 
    ;with sel as(
    select *,ROW_NUMBER() over(partition by name order by newid()) as row from @tab)
    select ID,name from sel where row=1
      

  7.   

    create table t 

    id int, 
    name varchar(10) 

    insert into t 
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' 
       
    select t.name,(SELECT TOP 1 id FROM t a WHERE a.NAME=t.NAME ORDER BY RAND()*100)id
    from t 
    group by t.name--RAND()*100   100为一个基数根据本身id列值得范围进行修改drop table t
      

  8.   


    CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
      

  9.   


    CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
    你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
    create table t  
    (  
    id int,  
    name varchar(10)  
    )  
    insert into t  
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' union
    select 6,'a' union
    select 7,'b' union
    select 8,'c' union
    select 9,'c' union
    select 10,'b' 
        
    select t.name,(SELECT TOP 1 id FROM t a WHERE a.NAME=t.NAME ORDER BY CHECKSUM(NEWID()))id 
    from t  
    group by t.name
        
    drop table t
      

  10.   

    租的服务器版本是SQL2000版本的,ROW_NUMBER()要报错喔..
      

  11.   

    很多书上都用CHECKSUM(NEWID())来生成随机数,而且测试数据里面看出也的确比较随机,不过要大数据量才明显,而你那个直接乘100或者其他数据,如果乘数不够大,那么重复的几率也不小.
      

  12.   


    CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
    你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
    create table t  
    (  
    id int,  
    name varchar(10)  
    )  
    insert into t  
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' union
    select 6,'a' union
    select 7,'b' union
    select 8,'c' union
    select 9,'c' union
    select 10,'b' 
        
    select t.name,(SELECT TOP 1 id FROM t a WHERE a.NAME=t.NAME ORDER BY CHECKSUM(NEWID()))id 
    from t  
    group by t.name
        
    drop table t我尝试了print CHECKSUM(NEWID()),你可以尝试一下,数据值非常大或者非常小。
      

  13.   


    CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
    你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
    create table t  
    (  
    id int,  
    name varchar(10)  
    )  
    insert into t  
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' union
    select 6,'a' union
    select 7,'b' union
    select 8,'c' union
    select 9,'c' union
    select 10,'b' 
        
    select t.name,(SELECT TOP 1 id FROM t a WHERE a.NAME=t.NAME ORDER BY CHECKSUM(NEWID()))id 
    from t  
    group by t.name
        
    drop table t我尝试了print CHECKSUM(NEWID()),你可以尝试一下,数据值非常大或者非常小。这样的值来排序不挺好吗?
    print CHECKSUM(NEWID())
    GO 100/*
    开始执行循环
    1581471204
    1348252016
    1996724381
    -1980841283
    -1564630166
    -1902451299
    -10457622
    1831699115
    -708607447
    2033937001
    -1915539893
    -484983626
    -1868583336
    356238476
    589645462
    937635699
    -1346684713
    1331966655
    1512007825
    1865893817
    -1063556626
    543179299
    -328760050
    -271327220
    1189543194
    -1510508131
    -118510832
    -355247300
    -1973015479
    644905109
    -1852408505
    18630223
    -845301686
    -1080522957
    1870927274
    291261448
    -180236195
    1816085609
    -56195279
    -1955612826
    -1823239235
    164804350
    464041650
    496189191
    -992863046
    1798310361
    -1122539631
    -1285659985
    -666769039
    -55363285
    -987353338
    -1434990918
    -1827311849
    -305431349
    -808734243
    185198454
    418573207
    1516953218
    1882558559
    -1850485502
    1695476463
    968570419
    -478270604
    1135814936
    4560807
    -977845055
    -1500413753
    1247482029
    -2029953002
    -143514433
    1413689305
    -89941
    -1830812283
    -1358386518
    2027708682
    -2091711200
    -860666170
    -298014650
    -1645890234
    1924256339
    -1106033668
    1664048751
    -1010700298
    1027476459
    1534224863
    -340552660
    1324855273
    -1527893151
    -2130199246
    -1740334248
    950218794
    2129991177
    -676034078
    853947593
    -1098546149
    -1000674271
    -2145996591
    1857163210
    -323222553
    638988058
    批处理执行已完成 100 次。*/
      

  14.   

    worf
     
    休闲所地,茶一杯。 文章 数据库优化 数据库基础 
    程序生成随机数与SQL语句生成随机数随机数可以通过程序生成,也可以通过SQL语句生成。通过程序生成随机数时一般采用硬件的编号+时间作为种子,这种方法在瞬间插入数据库N条数据的时候会影响随机数的效果,生成很多相邻的插入值相同。所以频繁插入时可以使用SQL语句的内置函数生成随机数,可以避免此类事情发生。
     
    Demo:
     
    ====================================================================================
     
    C#:
     
                int min=0,max=10;
     
                Random random = new Random();             int newNumber = random.Next(min, max);   //newNumber 取值在 min和max之间(0-10)
     
    T-SQL:
     
                select abs(checksum(newid()))%10    //生成随机数取值范围 0-9
     
    or
     
                select cast( floor(rand()*10) as int)   //生成随机数取值范围 0-10
     
    or
     
                select a+abs(checksum(newid()))%(b-a+1)   //生成随机数取值范围 a-b
      

  15.   


     --T-SQL:
     select abs(checksum(newid()))%10    //生成随机数取值范围 0-9
     select cast( floor(rand()*10) as int)   //生成随机数取值范围 0-10
     select a+abs(checksum(newid()))%(b-a+1)   //生成随机数取值范围 a-b
    URL:http://www.cnblogs.com/worfdream/articles/3045160.html
      

  16.   


    CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
    你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
    create table t  
    (  
    id int,  
    name varchar(10)  
    )  
    insert into t  
    select 1,'a' union
    select 2,'b' union
    select 3,'c' union
    select 4,'c' union
    select 5,'b' union
    select 6,'a' union
    select 7,'b' union
    select 8,'c' union
    select 9,'c' union
    select 10,'b' 
        
    select t.name,(SELECT TOP 1 id FROM t a WHERE a.NAME=t.NAME ORDER BY CHECKSUM(NEWID()))id 
    from t  
    group by t.name
        
    drop table t我尝试了print CHECKSUM(NEWID()),你可以尝试一下,数据值非常大或者非常小。这样的值来排序不挺好吗?
    print CHECKSUM(NEWID())
    GO 100/*
    开始执行循环
    1581471204
    1348252016
    1996724381
    -1980841283
    -1564630166
    -1902451299
    -10457622
    1831699115
    -708607447
    2033937001
    -1915539893
    -484983626
    -1868583336
    356238476
    589645462
    937635699
    -1346684713
    1331966655
    1512007825
    1865893817
    -1063556626
    543179299
    -328760050
    -271327220
    1189543194
    -1510508131
    -118510832
    -355247300
    -1973015479
    644905109
    -1852408505
    18630223
    -845301686
    -1080522957
    1870927274
    291261448
    -180236195
    1816085609
    -56195279
    -1955612826
    -1823239235
    164804350
    464041650
    496189191
    -992863046
    1798310361
    -1122539631
    -1285659985
    -666769039
    -55363285
    -987353338
    -1434990918
    -1827311849
    -305431349
    -808734243
    185198454
    418573207
    1516953218
    1882558559
    -1850485502
    1695476463
    968570419
    -478270604
    1135814936
    4560807
    -977845055
    -1500413753
    1247482029
    -2029953002
    -143514433
    1413689305
    -89941
    -1830812283
    -1358386518
    2027708682
    -2091711200
    -860666170
    -298014650
    -1645890234
    1924256339
    -1106033668
    1664048751
    -1010700298
    1027476459
    1534224863
    -340552660
    1324855273
    -1527893151
    -2130199246
    -1740334248
    950218794
    2129991177
    -676034078
    853947593
    -1098546149
    -1000674271
    -2145996591
    1857163210
    -323222553
    638988058
    批处理执行已完成 100 次。*/
    嗯 确实,没有问题。