sqlserver中根据表中的配置概率取到数据
create proc pr_zhanglei_test1
/*功能描述:
根据t_zhanglei_test1中perc设置的概率,取到相应数据old_id
*/
as
declare @percent_total int,
@max_id int,
@min_id int create table #t_zhanglei_temp --临时表存储变化表t_zhanglei_test1中total>0的数据
(id int identity(1,1) not null,
old_id int not null,
name varchar(50) not null,
total int not null,
perc int not null) insert into #t_zhanglei_temp(old_id,name,total,perc)
select id,name,total,perc
from t_zhanglei_test1
where total>0; if exists(select count(1) from #t_zhanglei_temp)
begin
declare @perc_temp int
select @max_id=max(id),@min_id=min(id),@percent_total=sum(perc)
from #t_zhanglei_temp create table #zhanglei_temp( --存储变化权值区间
id int not null,
old_id int not null,
start_num int not null,
end_num int not null
) insert into #zhanglei_temp(id,old_id,start_num,end_num)
select @min_id,old_id,1,perc
from #t_zhanglei_temp
where id=@min_id; declare @id int
declare @max_end_num int,
@old_id int
while @min_id<@max_id
begin set @min_id=@min_id+1; select @perc_temp =perc,@old_id=old_id
from #t_zhanglei_temp
where id=@min_id; select @max_end_num=max(end_num)
from #zhanglei_temp insert into #zhanglei_temp(id,old_id,start_num,end_num)
select @min_id,@old_id,@max_end_num+1,@max_end_num+@perc_temp; end declare @max_random int,
@random_temp int,
@return_id int
select @max_random=end_num
from #zhanglei_temp; set @random_temp=cast(ceiling(rand() * @max_random) as int); select @return_id=old_id
from #zhanglei_temp
where @random_temp
between start_num and end_num update t_total set total=total+1 where id=@return_id; if @@rowcount=0
begin
insert into t_total(id,total) values(@return_id,1);
end end --相关表结构 CREATE TABLE [t_zhanglei_test1] (
[id] [int] NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[total] [int] NOT NULL ,
[perc] [int] NOT NULL --本调数据出现的概率
) ON [PRIMARY]
GO --插入测试数据
insert into t_zhanglei_test1
select 111,'测试一',8,10
union all
select 222,'测试二',8,20
union all
select 333,'测试三',8,70 GO CREATE TABLE [t_total] (
[id] [int] NOT NULL ,
[total] [bigint] NOT NULL
) ON [PRIMARY]
GO -- 调取存储
declare @i int
set @i=0
while @i<10000
begin
exec pr_zhanglei_test1 set @i=@i+1
end --查看效果
select * from t_total
sqlserver中根据表中的配置概率取到数据的更多相关文章
- 在SQL SERVER中获取表中的第二条数据
在SQL SERVER中获取表中的第二条数据, 思路:先根据时间逆排序取出前2条数据作为一个临时表,再按顺时排序在临时表中取出第一条数据 sql语句如下: select top 1 * from(se ...
- 查看hive中某个表中的数据、表结构及所在路径
查看hive中action_data_myisam表中的数据.表结构及所在路径 1.客户端进入hive环境:hive 2.查看表数据,鉴于数据量大,这里只显示前五条:select * from act ...
- 备忘:MySQL中修改表中某列的数据类型、删除外键约束
-- MySQL中修改表中某列的数据类型 ALTER TABLE [COLUMN] 表名 MODIFY 列名 列定义; -- 删除外键约束 SHOW CREATE TABLE 表名; -- 复制CON ...
- SSAS中事实表中的数据如果因为一对多或多对多关系复制了多份,在维度上聚合的时候还是只算一份
SSAS事实表中的数据,有时候会因为一对多或多对多关系发生复制变成多份,如下图所示: 图1 我们可以从上面图片中看到,在这个例子中,有三个事实表Fact_People_Money(此表用字段Money ...
- C#中去掉表中重复的数据
/// <summary> /// 去掉表中重复的数据 int /// </summary> /// <param name="SourceTable&quo ...
- sql server 中删除表中数据truncate和delete的区别(转载自.net学习网)
我们都知道truncate table可以用来删除整个表的内容,它与delete后面不跟where条件的效果是一样.但除此之外,我们还清楚它们之间有其它的区别吗?本章我们将一起讨论truncate与d ...
- mysql中在表中insert数据时,有重复主键id时,变成update
MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDATE语法 例如: id name sex age 1 kathy male 23 2 Javer f ...
- MySQL 数据库中用户表中口令登陆设置
工具:MyEclipse8.5.apache-tomcat-6.0.43.MySQL5.6 问题:项目是同事发给我,正常运行之后,使用MySQL表里的管理员数据登陆时,提示“仅限于非总部工号登录!” ...
- pl/sql中误删表中数据并提交恢复办法
最近在操作表中数据时,删除了表中数据,但是又想恢复,后来查到了官方的一篇文档,发现还蛮有用的,如下: 在pl/sql中运行,select * from A as of TIMESTAMP to_tim ...
随机推荐
- Linux网络编程系列-常见疑惑
1.并发TCP最大连接数 一个TCP连接有一个四元组唯一标识{local_ip, local_port, remote_ip, remote_port} client端建立连接请求时,通常让系统分配一 ...
- esayui
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- css3圆环百分比,菜单栏定位导航
前段时间,社区个人中心改版,看了下设计图,当时隐约感觉到有两个地方(圆环百分比,菜单栏定位导航)比较麻烦.设计图大致如下: 首先看圆环百分比,网上的做法大致分两种,一种是用了CSS3中的transfo ...
- Android自定义控件之基本原理
前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...
- Node.js:DNS模块的使用
Nodejs的DNS模块包涵有关DNS查询和操作的方法,下面介绍该模块的基本用法以及实现一个DNS查询小工具. 1.获取DNS服务器地址 使用getServers方法,该方法返回一个IP地址组成的数组 ...
- 后台管理UI的选择
最近要做一个企业的OA系统,以前一直使用EasyUI,一切都好,但感觉有点土了,想换成现在流行的Bootstrap为基础的后台UI风格,想满足的条件应该达到如下几个: 1.美观.大方.简洁 2.兼容I ...
- LeetCode Online Judge 1. Two Sum
刷个题,击败0.17%... Given an array of integers, return indices of the two numbers such that they add up t ...
- C# 系统托盘图标
C# 系统托盘图标 WPF NotifyIcon 资料 网址: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon http://www. ...
- wxWidgets
wxWidgets Code::Blocks环境 Code::Blocks下载: Code::Blocks使用: codeblocks-16.01mingw-setup.exe 它的gcc版本为4.9 ...