当前位置: 代码迷 >> Sql Server >> 表中记录为一条,怎么查询才能得到多条记录?即一条变多条
  详细解决方案

表中记录为一条,怎么查询才能得到多条记录?即一条变多条

热度:53   发布时间:2016-04-27 19:10:25.0
表中记录为一条,如何查询才能得到多条记录?即一条变多条
以下问题,请教高人,谢谢了。
SQL code
--表中记录/*Num    Count2  Code92126  200     '100202'94752  100     '100202'19     200     '100201'20     100     '100201'*/--希望得到的查询结果,即希望Num>90000分拆成相应的几条/*Num Count2 Code21   200   '100202'22   200   '100202'23   200   '100202'24   200   '100202'25   200   '100202'26   200   '100202'47   100   '100202'48   100   '100202'49   100   '100202'50   100   '100202'51   100   '100202'52   100   '100202'19   200   '100201'20   100   '100201'*/


------解决方案--------------------
SQL code
--参考:select b.Number Num,a.count2,a.codefrom tb aleft join master..spt_values bon a.Num>90000 and b.type='p'and b.number between substring(rtrim(a.Num),2,2) and right(a.Num,2)union allselect Num,count2,codefrom tbwhere Num<=900000
------解决方案--------------------
SQL code
-----------------------------------  Author: htl258(Tony)--  Date  : 2009-08-05 14:08:24-----------------------------------> 生成测试数据表:tbIf not object_id('[tb]') is null    Drop table [tb]GoCreate table [tb]([Num] int,[Count2] int,[Code] varchar(10))Insert tbSelect 92126,200,'100202' union allSelect 94752,100,'100202' union allSelect 19,200,'100201' union allSelect 20,100,'100201'Go--Select * from tb-->SQL查询如下:select b.number num,a.[Count2],a.code from tb a     join master..spt_values b         on b.type='p' and a.num>90000        and b.number>=(a.num-90000)/100        and b.number<=(a.num-90000)%100union allselect * from tb where num<90000/*num         Count2      code----------- ----------- ----------21          200         10020222          200         10020223          200         10020224          200         10020225          200         10020226          200         10020247          100         10020248          100         10020249          100         10020250          100         10020251          100         10020252          100         10020219          200         10020120          100         100201(14 行受影响)*/
  相关解决方案