从表中随机选取记录的问题

goodsong 2004-09-11 09:39:31
table1
col1 col2
1 a
1 b
1 a
2 c
2 a
2 b
2 a
3 d
3 e
. .
选取规则
根据col1的值的不同,不同的值各随机取一条记录
上例中的一个合乎要求的结果为
1 b
2 a
3 e
有没有办法?
...全文
193 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
tx1icenhe 2004-09-11
  • 打赏
  • 举报
回复
select DISTINCT col1,col2=(select top 1 col2 from TABLE1 where col1=a.col1 order by newid())
from TABLE1 a

也可以
zicxc 2004-09-11
  • 打赏
  • 举报
回复
不好意思,我把表名搞错了

select col1,col2=(select top 1 col2 from TABLE1 where col1=a.col1 order by newid())
from TABLE1 a
group by col1
是可以的
zicxc 2004-09-11
  • 打赏
  • 举报
回复
你的不行
select col1,col2=(select top 1 col2 from 表 where col1=a.col1 order by newid())
from TABLE1 a
group by col1
出错
select col1,col2=(select top 1 col2 from 表 where col1=a.col1 order by newid())
from TABLE1 a
结果:
col1 col2
----------- ----------
1 a
1 b
1 a
2 c
2 a
2 b
2 a
3 d
3 e

(所影响的行数为 9 行)

这样用DISTINCT也不行了
zicxc 2004-09-11
  • 打赏
  • 举报
回复
呵呵,现在我觉得是对的
估计每次查询,同一条记录产生的newid()在子查询或者嵌套查询里的和外面的都是一样的,这样就保证这个语句的正确了


zjcxc 2004-09-11
  • 打赏
  • 举报
回复
--原来测试数据看错了,所以想的这一种方法也是可行的(一句查询)

select col1,col2=(select top 1 col2 from 表 where col1=a.col1 order by newid())
from 表 a
group by col1
zjcxc 2004-09-11
  • 打赏
  • 举报
回复
不对,我看错测试了.
zjcxc 2004-09-11
  • 打赏
  • 举报
回复
--冒牌,你的有大问题

--测试

--测试数据
declare @t table(col1 int,col2 varchar(10))
insert @t select 1,'a'
union all select 1,'b'
union all select 1,'a'
union all select 2,'cc'
union all select 2,'aa'
union all select 2,'bb'
union all select 2,'aaa'
union all select 3,'ddd'
union all select 3,'eee'

--查询
select id=identity(int),* into #t
from @t

select * from #t a
where id=(select top 1 id from #t where col1=a.col1 order by newid())
drop table #t

/*--测试结果

id col1 col2
----------- ----------- ----------
1 1 a
7 2 aaa --这个明显不对
9 3 eee

(所影响的行数为 3 行)
--*/
zicxc 2004-09-11
  • 打赏
  • 举报
回复
斑竹帮忙测试下,感觉好像有点问题,但是测试结果却没发现问题
goodsong 2004-09-11
  • 打赏
  • 举报
回复
谢谢!之所以麻烦各位是为了求得正确的结果
目前测试数据不足,我自己只有办法求出结果,没有办法保证结果的正确性
更没有办法保证我的方法是比较好的
我对SQL 用得不多,也没经验
因此想看看大家是怎么解决的
zicxc 2004-09-11
  • 打赏
  • 举报
回复
斑竹的当然没问题,但是如果:

select id=identity(int),* into #t
from table1

select * from #t a
where id=(select top 1 id from #t where col1=a.col1 order by newid())

有没问题呢?
测试没发现问题
zjcxc 2004-09-11
  • 打赏
  • 举报
回复
而且col2有重复,所以理论上也行不通.
zicxc 2004-09-11
  • 打赏
  • 举报
回复
真的有问题
select * from table1 a
where col2=(select top 1 col2 from table1 where col1=a.col1 order by newid())

有时返回3条,有时返回4条
zjcxc 2004-09-11
  • 打赏
  • 举报
回复
一楼的那种是不行的.
zjcxc 2004-09-11
  • 打赏
  • 举报
回复
--测试

--测试数据
declare @t table(col1 int,col2 varchar(10))
insert @t select 1,'a'
union all select 1,'b'
union all select 1,'a'
union all select 2,'c'
union all select 2,'a'
union all select 2,'b'
union all select 2,'a'
union all select 3,'d'
union all select 3,'e'

--查询
select id=identity(int),* into #t
from(select top 100 percent * from @t order by newid())a
select col1,col2
from #t a,(select id=min(id) from #t group by col1)b
where a.id=b.id
order by col1

drop table #t

/*--测试结果

col1 col2
----------- ----------
1 a
2 a
3 d

(所影响的行数为 3 行)
--*/
zjcxc 2004-09-11
  • 打赏
  • 举报
回复
--查询
select id=identity(int),* into #t
from(select top 100 percent * from 原表 order by newid())a
select col1,col2
from #t a,(select id=min(id) from #t group by col1)b
where a.id=b.id
order by col1

drop table #t
zicxc 2004-09-11
  • 打赏
  • 举报
回复
select * from table1 a
where id=(select top 1 id from table1 where col1=a.col1 order by newid())

理论上好像这样,没测试,估计有问题
luckljtchinaren 2004-09-11
  • 打赏
  • 举报
回复
用rand函数应该也可以实现吧?
先试试...
goodsong 2004-09-11
  • 打赏
  • 举报
回复
牛阿,谢谢了!

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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