日期:2014-05-18 浏览次数:21039 次
--SQL2005環境:
;with NewYear
as
(select 1 as ID
union all
select ID+1 as ID from NewYear where ID<888--以888為例
),NewYear168
as
(select
*,(ID-1)/88 as ID2
from NewYear
where (ID/8)=ID*1.0/8
),NewYear2010
as
(
select
top 88 t2.ID as [Floor],
rtrim((t2.ID-1)%188+1) as Qty --188可為吉祥數字
from
(select distinct ID2 from NewYear168)t
cross apply
(select top 10 * from NewYear168 where ID2=t.ID2 order by NewID())t2
order by newID())
select
[Floor]as 樓層,
cast(stuff(replace([Qty],'4','8'),len([Qty]),1,'8') as int) as 中獎紅包
from NewYear2010
order by 1
option(MAXRECURSION 0)
--把結尾數改為8,把中間有其它數字有4的改為8。
樓層 中獎紅包 8 8 24 28 32 38 40 88 64 68 88 88 96 98 104 108 112 118 120 128 128 128 136 138 152 158 160 168 168 168 176 178 184 188 192 8 208 28 216 28 232 88 240 58 248 68 256 68 264 78 272 88 288 108 296 108 312 128 336 188 344 158 368 188 376 188 384 8 392 18 400 28 408 38 416 88 424 88 432 58 440 68 448 78 456 88 464 88 480 108 488 118 496 128 504 128 512 138 520 188 528 158 536 168 544 168 552 178 560 188 568 8 576 18 600 38 608 88 616 58 624 68 632 68 648 88 656 98 664 108 672 108 680 118 688 128 696 138 704 188 720 158 728 168 736 178 744 188 752 188 760 8 776 28 784 38 792 88 808 58 816 68 824 78 832 88 848 98 856 108 864 118 880 128 888 138
--SQL2005環境:
if object_id('Tempdb..#NewYear2208') is not null
drop table #NewYear2208
;with NewYear
as
(select 889 as ID
union all
select ID+1 as ID from NewYear where ID<2208--以2208樓
),NewYear168
as
(select
*,(ID-889)/88 as ID2
from NewYear
where (ID-888)/8=(ID-888)*1.0/8
)
select
t2.ID as [Floor]
,Qty=case row_Number()over(partition by t.ID2 order by newID()) when 1 then 188 when 2 then 88 when 3 then 68 else 0 end
into #NewYear2208
from
(select distinct ID2 from NewYear168)t
cross apply
(select top 8 * from NewYear168 where ID2=t.ID2 order by NewID()
)t2
option(MAXRECURSION 0)
;with HappyNewYear
as
(
select
[Floor],NewRow=row_Number()over(order by newID())
from
#NewYear2208
where Qty=0
)
,NewYear2010
as
(
select * from #NewYear2208 where Qty>0
union all
select [Floor],
Qty=case when NewRow<=3 then 168
when NewRow<=6 then 118
when NewRow<=7 then 108
when NewRow<=8 then 38
when NewRow<=9 then 28
else ((abs(checksum(newID()))-1)-1)%18+1 end
from HappyNewYear
)
select
[Floor]as 樓層,cast(stuff([Qty],len([Qty]),1,'8') as int) as 中獎紅包
from NewYear2010
order by 1
推荐阅读更多>
-
怎么把BI Development Studio 2005和Visual Studio2010融合到一起
-
取最大八小时平均最大值
-
怎么在数据库中实现定时插入数据
-
请问SQL2000在手动备份后就无法自动备份了
-
请问一个基础有关问题类似@projectname的定义中@后面定义的只能用英文字母吗?可以用其他特殊字符吗
-
求详细解释。解决思路
-
有没有解密SQL Server 2008中被加密的触发器的代码或工具?解决思路
-
恢复数据库后怎么关联用户和该用户的存储过程
-
小弟我的powerdesigner16.5突然不能用了,网上各种破解补丁都不好使
-
求一段sql 拼接方法 双循环 存储过程解决办法
-
microsoft sql 企业管理器 建表时出现异常:mmc检测到此管理单元发生一个异常 建议您关闭并重新启动mmc
-
,关于两张表的匹配有关问题
-
查询分析器插入的中文变成'?'的有关问题
-
查询过滤重复有关问题,怎么可以过滤重复时,取ID较大的值
-
SQL怎么添加超链接数据
-
视图中怎么自动根据要求生成一个列,并填充数据
-
哪位帮忙看看,该怎么解决
-
用powerdesigner 12无法将PDM直接生成到连接得数据啊解决方法
-
一个报表,但很难,根据数据之间的关系排序,求查询语句
-
sql语句查询去除重复不用distinct用having解决办法