求教MSSQL分组后每组随机取出一条数据怎么写,要随机取的..

liu_lxx 2013-04-26 12:20:57
字段1 字段2
id name
1 a
2 b
3 c
4 c
5 b
根据name分组,最后每组随机取出一条数据。

网上有许多类似的代码,但是运行有问题啊

...全文
768 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010484175 2013-04-27
  • 打赏
  • 举报
回复
最后一次在这个垃圾站弄
hgwyl 2013-04-26
  • 打赏
  • 举报
回复
思路可以有 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
上高土著 2013-04-26
  • 打赏
  • 举报
回复
谢谢楼主。。。。。。。。。。
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复
引用 19 楼 DBA_Huangzj 的回复:
[quote=引用 18 楼 daiyueqiang 的回复:] [quote=引用 12 楼 DBA_Huangzj 的回复:] [quote=引用 11 楼 daiyueqiang 的回复:] [quote=引用 8 楼 DBA_Huangzj 的回复:] 改一下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
CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。[/quote] 你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
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
[/quote] 我尝试了print CHECKSUM(NEWID()),你可以尝试一下,数据值非常大或者非常小。[/quote] 这样的值来排序不挺好吗?
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 次。

*/
[/quote] 嗯 确实,没有问题。
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复

 --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
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复
引用 14 楼 DBA_Huangzj 的回复:
很多书上都用CHECKSUM(NEWID())来生成随机数,而且测试数据里面看出也的确比较随机,不过要大数据量才明显,而你那个直接乘100或者其他数据,如果乘数不够大,那么重复的几率也不小.
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
發糞塗牆 2013-04-26
  • 打赏
  • 举报
回复
引用 18 楼 daiyueqiang 的回复:
[quote=引用 12 楼 DBA_Huangzj 的回复:] [quote=引用 11 楼 daiyueqiang 的回复:] [quote=引用 8 楼 DBA_Huangzj 的回复:] 改一下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
CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。[/quote] 你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
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
[/quote] 我尝试了print CHECKSUM(NEWID()),你可以尝试一下,数据值非常大或者非常小。[/quote] 这样的值来排序不挺好吗?
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 次。

*/
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复
引用 12 楼 DBA_Huangzj 的回复:
[quote=引用 11 楼 daiyueqiang 的回复:] [quote=引用 8 楼 DBA_Huangzj 的回复:] 改一下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
CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。[/quote] 你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
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
[/quote] 我尝试了print CHECKSUM(NEWID()),你可以尝试一下,数据值非常大或者非常小。
qietingfengdeyanse 2013-04-26
  • 打赏
  • 举报
回复
不谢 不谢 小子 继续加油~~~
liu_lxx 2013-04-26
  • 打赏
  • 举报
回复
引用 10 楼 daiyueqiang 的回复:
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
这方法也可以,谢谢你们的帮忙啦!!
liu_lxx 2013-04-26
  • 打赏
  • 举报
回复
引用 8 楼 DBA_Huangzj 的回复:
改一下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
测试成功了,谢谢版主大人..
發糞塗牆 2013-04-26
  • 打赏
  • 举报
回复
很多书上都用CHECKSUM(NEWID())来生成随机数,而且测试数据里面看出也的确比较随机,不过要大数据量才明显,而你那个直接乘100或者其他数据,如果乘数不够大,那么重复的几率也不小.
liu_lxx 2013-04-26
  • 打赏
  • 举报
回复
引用 9 楼 tangguangqiang 的回复:

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

租的服务器版本是SQL2000版本的,ROW_NUMBER()要报错喔..
發糞塗牆 2013-04-26
  • 打赏
  • 举报
回复
引用 11 楼 daiyueqiang 的回复:
[quote=引用 8 楼 DBA_Huangzj 的回复:] 改一下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
CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。[/quote] 你确定?多执行几次看看是不是真的不是第一就是最后?我执行的结果不是你说的那样
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
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复
引用 8 楼 DBA_Huangzj 的回复:
改一下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
CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复
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
习惯性蹭分 2013-04-26
  • 打赏
  • 举报
回复

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

發糞塗牆 2013-04-26
  • 打赏
  • 举报
回复
改一下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
liu_lxx 2013-04-26
  • 打赏
  • 举报
回复
引用 6 楼 daiyueqiang 的回复:
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.name

drop table t
这样是取ID最大的吧,我要随机的额..
daiyueqiang2045 2013-04-26
  • 打赏
  • 举报
回复
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.name

drop table t
加载更多回复(4)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧