sql-rand()随机函数使用方法介绍
2021/8/1 19:39:12
本文主要是介绍sql-rand()随机函数使用方法介绍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、随机取数
MySql或Hive数据库,从样本数据中随机提取数据表中的N条记录,可以使用rand()函数,结合使用order by对数据排序,用limit N取topN
select * from TableName order by rand() limit N
案例:每月信用卡支出总额超过1000美元的客户有机会赢得抽奖。假设您在2019年10月从所有符合条件的客户中随机选择100名幸运客户,您是否可以编写一个查询以返回“user_id”列表作为此次抽奖的结果?
--Task 1. Lucky Draw select user_id, substr(create_time, 1, 7) month, sum(amount) amount_sum from card_exp where substr(create_time, 1, 7) = ’2019-10’ group by user_id, substr(create_time, 1, 7) having amount_sum > 1000 order by rand() limit 100
2、生成随机数
随机数函数: rand()
语法: rand(),rand(int seed)
返回值: double
说明:返回值是一个0到1范围内的随机数。如果指定种子seed,则会等到一个不变的随机数
--不指定种子 hive> select rand() from tabel_name; 0.5577432776034763 hive> select rand() from tabel_name; 0.6638336467363424 --指定随机数种子为1 hive> select rand(1) from tabel_name; 0.7220096548596434 hive> select rand(1) from tabel_name; 0.7220096548596434
这篇关于sql-rand()随机函数使用方法介绍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-19环境变量处理课程:新手入门教程
- 2024-09-19接口模块封装课程:新手入门指南
- 2024-09-19请求动作封装课程:新手入门教程
- 2024-09-19拖拽表格课程:新手入门指南
- 2024-09-19页面权限课程:新手必学的权限管理入门教程
- 2024-09-19如何正确主动登出课程:新手必读教程
- 2024-09-19Element-Plus课程:新手入门与初级教程
- 2024-09-19Token处理入门教程:新手必看指南
- 2024-09-19如何应对被动登出课程的情况:新手必读指南
- 2024-09-19打包优化课程:初学者的必备指南