字段1 字段2
id name
1 a
2 b
3 c
4 c
5 b
根据name分组,最后每组随机取出一条数据。网上有许多类似的代码,但是运行有问题啊MS SQL分组后随机取出数据
id name
1 a
2 b
3 c
4 c
5 b
根据name分组,最后每组随机取出一条数据。网上有许多类似的代码,但是运行有问题啊MS SQL分组后随机取出数据
解决方案 »
- 按月份显示数据的值 查询某月的值
- 求如何把两条sql结果联合起来。。。。急。
- 关于SELECT语句的
- 合计记录的问题
- 求救,求救!!!!! IF @@TRANCOUNT > 0 COMMIT TRAN 老是 BLOCKING 要多少分都可以!!!!
- 语音搜索如何实现
- 100分求教一个解密问题,不知道是什么编码的!!谢谢
- 如何写一个语句将一个文本文件的内容,全部插入一个text字段中
- 简单问题!大家都来看看,第一个解决的就全部给了~~
- 这个SQL语句怎么写?
- select * from (@test2) 提示必须声明变量 '@test2',请问应该怎么写
- XP系统的登陆密码会写到sql server 2000里的XP系统数据库表吗?
2、1—4的范围内获得一个随机数,赋值给checki
3、split(name,",")(checki)
4、如果内容分组后,第一组和第二组的数量不一致——"a,b,c,d"和"e,f,g",用下标对比处理一下checki
(
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
(
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
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
(
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
CHECKSUM 用于生成哈希索引,产生的值一般很大或者很小,所以产生的就不是第一个就是最后一个。
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
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()),你可以尝试一下,数据值非常大或者非常小。
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 次。*/
休闲所地,茶一杯。 文章 数据库优化 数据库基础
程序生成随机数与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
--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
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 次。*/
嗯 确实,没有问题。