=================================版权声明=================================

版权声明:原创文章 谢绝转载 

请通过右侧公告中的“联系邮箱(wlsandwho@foxmail.com)”联系我

勿用于学术性引用。

勿用于商业出版、商业印刷、商业引用以及其他商业用途。                   

 

本文不定期修正完善。

本文链接:http://www.cnblogs.com/wlsandwho/p/5075789.html

耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html

=======================================================================

没啥说的,鄙视那些无视版权随意抓取博文的爬虫小网站站长,圣诞了,祝你们见到上帝。

=======================================================================

=======================================================================

从来只见过有人要用distinct去重,可没听说过谁还要生成重复数据的。今儿咱算是开了眼了。

上代码吧。

 1 --by WLS
 2 --20151224
 3 --网络代码有风险复制粘贴须谨慎
 4 USE tempdb
 5 GO
 6 
 7 IF OBJECT_ID (N't_TestbyWLS', N'U') IS NOT NULL
 8 DROP TABLE t_TestbyWLS;
 9 CREATE TABLE t_TestbyWLS(Id INTEGER PRIMARY KEY,Name NVARCHAR(15),Qty INTEGER)
10 GO
11 
12 INSERT INTO t_TestbyWLS VALUES
13 (1,'thbytwo',3),
14 (2,'wlsandwho',2)
15 GO
16 
17 SELECT * FROM t_TestbyWLS
18 GO
19 
20 -------------------------------------------------------------------------------------------
21 WITH TempQty
22 AS
23 (
24     SELECT number AS Num FROM master..spt_values WHERE type='p'
25 )
26 SELECT a.Id,a.Qty FROM t_TestbyWLS a LEFT JOIN TempQty b ON b.Num>0 AND b.Num<=a.Qty
27 GO

分步思路

 1 -------------------------------------------------------------------------------------------
 2 --step0
 3 SELECT number AS Num FROM master..spt_values WHERE type='p'
 4 GO
 5 --step1
 6 WITH TempQty
 7 AS
 8 (
 9     SELECT number AS Num FROM master..spt_values WHERE type='p'
10 )
11 SELECT a.Id,a.Qty,b.Num FROM t_TestbyWLS a LEFT JOIN TempQty b ON 1=1
12 --step2
13 WITH TempQty
14 AS
15 (
16     SELECT number AS Num FROM master..spt_values WHERE type='p'
17 )
18 SELECT a.Id,a.Qty,b.Num FROM t_TestbyWLS a LEFT JOIN TempQty b ON 1=1 AND b.Num>0 AND b.Num<=a.Qty
19 --step3
20 WITH TempQty
21 AS
22 (
23     SELECT number AS Num FROM master..spt_values WHERE type='p'
24 )
25 SELECT a.Id,a.Qty FROM t_TestbyWLS a LEFT JOIN TempQty b ON b.Num>0 AND b.Num<=a.Qty
26 GO

执行结果

执行计划

=======================================================================

本着从业务出发的原则,我问了下事主为何有这种浪(wéi)费(bèi)空(lì)间(shǐ)、低(bù)效(rén )率(dào )、违(pò)反(huài)数(rì)据(nèi)库(wǎ)范(tiáo)式(yuē)的需求,

下面是问答截图。

看来是软件架构设计的问题了,不予置评。

=======================================================================

(友情支持请扫描这个)

微信扫描上方二维码捐赠

 

20160406-01

顺便扩展下