要求随机取3条记录.要求这3条记录的平均值在5到8之间.......
随机取3条可以写..现在考虑是取3条出来,判断其平均值,
如果符合则输出,8符合则重新取..直到符合...问题是.需要建1临时个表存一下取出的3条记录不?
....
有别的思路更好~~谢谢。.
随机取3条可以写..现在考虑是取3条出来,判断其平均值,
如果符合则输出,8符合则重新取..直到符合...问题是.需要建1临时个表存一下取出的3条记录不?
....
有别的思路更好~~谢谢。.
解决方案 »
- Sql Server Browser启动后又停止了,它没有什么可做的 ,例如性能计数服务
- SQL2005转为sql2000时,发生两个问题
- 怎么让一个表每个列循环?
- 有关ibatis事务问题
- 数据库升级的问题,大家帮帮忙
- visio 里面建立数据库模型时, 执行反向工程来显示数据库里面的情况时,数据库里面的表就会添加到页面上,但数据库里面的存储过程不会?
- sql server2008安装问题 急~!求帮助
- 多线程连接数据库出现java.lang.NullPointerException
- 谁能给我解释一下这条语句(不难,送分20),只是我不会。
- 一个关于MySQL的问题! 加急!!
- 求教~~~
- 一个很复杂的函数
set @flag = 20 -- 尝试20次
while @flag > 0
begin
if object_id('tempdb..#t') is not null
drop table #t
select top 3 * into #t from tb order by newid()
if (select avg(计算列) from #t) between 5 and 8
set @flag = 0
set @flag = @flag - 1
end
if @flag = -1
select * from #t
else
raiserror('没有满足条件的记录', 16, 1)
set nocount ondeclare @t int
declare @f float
set @t=1
while @t<21
begin
select id into #t from table1 order by newid()
set @f = (select avg(id) from #t)
if @f between 5 and 8
break
set @t=@t + 1
drop table #t
end
if @t < 21
select * from #t
else
select ''
where a.id<>b.id and b.id<>c.id and a.id<25 and b.id<25 and c.id<25
group by a.id,b.id,c.id
having sum(a.id+b.id+c.id)/3 between 5 and 8
order by newid()
where a.id<>b.id and b.id<>c.id and a.id<25 and b.id<25 and c.id<25
group by a.id,b.id,c.id
having sum(a.id+b.id+c.id)/3 between 5 and 8
--这样可以遍历在table1表符合条件的所有组合,然后在这个结果中取其中一条即可:
select top 1 第一个ID=a.id,第二个ID=b.id,第三个ID=c.id,平均值=(a.id+b.id+c.id)/3 from table1 a join table1 b on 1=1 join table1 c on 1=1
where a.id<>b.id and b.id<>c.id and a.id<25 and b.id<25 and c.id<25
group by a.id,b.id,c.id
having sum(a.id+b.id+c.id)/3 between 5 and 8
order by newid()
--order by newid()随机排序,top 1取随机排序的第一条·!