求租:随机取数的sql语句

hyctlxs 2008-04-09 09:15:38
请教SQL: 比如表a里面有 1,2,3,80,200,3000 等列数字 ;现在要随机取3个数相加,结果最接近100 这样的sql怎末写?
...全文
170 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sp4 2008-04-11
  • 打赏
  • 举报
回复
呵呵,不是随机。是计算获取列和与100的差的最小值而已,就是一个计算方式。

不过这类计算虽然可以实现,但是这类需求对于已存在的表直接计算操作是很无奈的事。可以说需求的结果比较BT
fcuandy 2008-04-09
  • 打赏
  • 举报
回复
穷举.
否则你得到的不是随机:D
wzy_love_sly 2008-04-09
  • 打赏
  • 举报
回复
用笛卡耳积,匹配所有项,查找3个列相加小于100的 top 1
hyctlxs 2008-04-09
  • 打赏
  • 举报
回复
谢谢两位!!!!这个困扰我多天的问题,终于处理了。。。。
dawugui 2008-04-09
  • 打赏
  • 举报
回复
create table A(col int)
insert into A values(1)
insert into A values(2)
insert into A values(3)
insert into A values(80)
insert into A values(200)
insert into A values(3000)
go

select m1.col , m2.col , m3.col from A m1,A m2,A m3 ,
(
select mincol = min(abs(t1.col + t2.col + t3.col - 100))
from A t1,A t2,A t3
where t1.col < t2.col and t2.col < t3.col
) t
where m1.col < m2.col and m2.col < m3.col and abs(m1.col + m2.col + m3.col - 100) = t.mincol

drop table A

/*
col col col
----------- ----------- -----------
2 3 80

(所影响的行数为 1 行)
*/
dawugui 2008-04-09
  • 打赏
  • 举报
回复
假设A表的列为col

select m1.col , m2.col , m3.col from A m1,A m2,A m3 ,
(
select mincol = min(abs(t1.col + t2.col + t3.col - 100))
from A t1,A t2,A t3
where t1.col < t2.col and t2.col < t3.col
) t
where abs(m1.col + m2.col + m3.col - 100) = t.mincol
hyctlxs 2008-04-09
  • 打赏
  • 举报
回复
我只知道sql里面随机取数的是 order by newid() 现在关键是不知道怎末取数相加 与100 比较,然后系统列出 sum后的和最接近100 的所有行;
wzy_love_sly 2008-04-09
  • 打赏
  • 举报
回复
这不是随机的问题,

34,838

社区成员

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

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