=================================版權聲明=================================
版權聲明:原創文章 謝絕轉載?
請通過右側公告中的“聯系郵箱(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ē)的需求,
下面是問答截圖。
看來是軟件架構設計的問題了,不予置評。
=======================================================================
(友情支持請掃描這個)
微信掃描上方二維碼捐贈
?