免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3704 | 回复: 8
打印 上一主题 下一主题

多表取随机记录问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-19 17:53 |只看该作者 |倒序浏览
单表取随机记录可以用:
select * from tablename order by rand() fetch first n rows only

现有表A 内容为:
xm
张三
李四
王五
表B 内容为其他数据

现求:
表A  XM字段每一行取表B  5个随机字段
即张三,李四,王五各取表B 5个随机字段

[ 本帖最后由 haiyuey 于 2007-7-23 14:47 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-07-19 18:50 |只看该作者
查row_number函数用法
取前五位

论坛徽章:
0
3 [报告]
发表于 2007-07-19 18:52 |只看该作者
请写出过程,谢谢!!!

论坛徽章:
0
4 [报告]
发表于 2007-07-23 08:12 |只看该作者
谁能帮帮我!

论坛徽章:
0
5 [报告]
发表于 2007-07-26 14:32 |只看该作者
等了好几天了

论坛徽章:
0
6 [报告]
发表于 2007-07-26 14:59 |只看该作者
原帖由 haiyuey 于 2007-7-26 14:32 发表
等了好几天了


google

论坛徽章:
0
7 [报告]
发表于 2007-07-26 18:18 |只看该作者
google 没找到

论坛徽章:
0
8 [报告]
发表于 2007-08-21 21:33 |只看该作者
老兄你那个是错的。
误人子弟

论坛徽章:
11
数据库技术版块每日发帖之星
日期:2016-06-25 06:20:00数据库技术版块每日发帖之星
日期:2016-06-24 06:20:00数据库技术版块每日发帖之星
日期:2016-05-03 06:20:00数据库技术版块每日发帖之星
日期:2016-04-21 06:20:00数据库技术版块每日发帖之星
日期:2016-01-23 06:20:00数据库技术版块每日发帖之星
日期:2015-12-03 06:20:00综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-19 06:20:00数据库技术版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-07-30 06:20:00
9 [报告]
发表于 2007-08-22 09:41 |只看该作者
可以去看看SELECT的TABLESAMPLE子句或许能满足你的需要

Example 1: Request a 10% Bernoulli sample of the Sales table for auditing purposes.

SELECT * FROM Sales TABLESAMPLE BERNOULLI(10)

Example 2: Compute the total sales revenue in the Northeast region for each product category, using a random 1% SYSTEM sample of the Sales table. The semantics of SUM are for the sample itself, so to extrapolate the sales to the entire Sales table, the query must divide that SUM by the sampling rate (0.01).

SELECT SUM(Sales.Revenue) / (0.01) FROM Sales TABLESAMPLE SYSTEM(1) WHERE Sales.RegionName = ’Northeast’ GROUP BY Sales.ProductCategory

Example 3: Using the REPEATABLE clause, modify the previous query to ensure that the same (yet random) result is obtained each time the query is executed. (The value of the constant enclosed by parentheses is arbitrary.)

SELECT SUM(Sales.Revenue) / (0.01) FROM Sales TABLESAMPLE SYSTEM(1) REPEATABLE(3578231) WHERE Sales.RegionName = ’Northeast’ GROUP BY Sales.ProductCategory
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP