SQL 实现随机双色球( 随机生成6个红球,蓝球按顺序出现)

        每次生成16注或16的倍数,每期必中一个蓝球,最少中5元,花费 27元

SQL代码

-- 随机生成红球,蓝球按顺序生成
create table #BicolorBallRandom(
	id int IDENTITY
	, room varchar(20)
	, roomNum int
	, color varchar(20)
	, colorNum int
	, Red DECIMAL (38, 10)
)

create table #BicolorBallRandomResult(
	id int IDENTITY
	, room varchar(20)
	, roomNum int
	, color varchar(20)
	, colorNum int
	, Red DECIMAL (38, 10)
)
declare @k int
declare @i int

set @k = 1
set @i = 1
-- 随机生成6个红球,蓝球按顺序生成
while @k <= 16
begin
	while @i< 7
		begin
			declare @red int,@blue int
			select @red = cast( ceiling(rand()*33) as int)	
			if not exists(select 1 from #BicolorBallRandom where Red = @red and roomNum = @k)
				begin 
					insert into #BicolorBallRandom
					select '第'+convert(varchar(20),@k)+'组',@k,'红',@i, @red
					set @i = @i + 1
				end
			else
				begin
					set @i = @i
				end
			if(@i = 7)
				begin
					if((@k % 16) = 0)
					begin
						insert into #BicolorBallRandom
						select '第'+convert(varchar(20),@k)+'组',@k,'蓝',7, 16
					end
					else
					begin
						insert into #BicolorBallRandom
						select '第'+convert(varchar(20),@k)+'组',@k,'蓝',7, @k % 16
					end
				end
		end
	set @i = 1
	set @k = @k + 1
	
end

-- 展示,红球按大小顺序排序
insert into #BicolorBallRandomResult
select room	,roomNum	,color	,(ROW_NUMBER() OVER(PARTITION BY roomNum ORDER BY roomNum,color,Red)) colorNum	,Red from #BicolorBallRandom order by roomNum,color,Red

select
sum(roomNum) / 7  id
,room
,sum(Red1) 红1
,sum(Red2) 红2
,sum(Red3) 红3
,sum(Red4) 红4
,sum(Red5) 红5
,sum(Red6) 红6
,sum(Blue) 蓝

from (
	select 
	room
	,roomNum
	,case when colorNum = 1 then Red else 0 end  Red1
	,case when colorNum = 2 then Red else 0 end  Red2
	,case when colorNum = 3 then Red else 0 end  Red3
	,case when colorNum = 4 then Red else 0 end  Red4
	,case when colorNum = 5 then Red else 0 end  Red5
	,case when colorNum = 6 then Red else 0 end  Red6
	,case when colorNum = 7 then Red else 0 end  Blue
	from #BicolorBallRandomResult 
)a
group by room,roomNum
order by roomNum
-- 删除临时表
drop table #BicolorBallRandom
drop table #BicolorBallRandomResult

成果展示

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白雨青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值