Avatar of nutnut
nutnutFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Rows split to columns

Hi

I have a table that has 2 cols From and To

The values are say from=1 and to=9 and in the second row from=10 and to=20

I need SQL to turn that into 1 column listing 1,2,3,4,5,6,7,8,910,11,12,13,14,15,16,17,18,19,20

Thanks
Microsoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
nutnut
Avatar of PortletPaul
PortletPaul
Flag of Australia image

can you provide a sample of the data and specify the table/fields?
are [from] and [to] fields integers?
are there other fields in the table to "group by"?
and, would there ever be gaps in the output number sequence (if so when)?

e.g.
category-A| 1,2,3,4,5,6
category-B| 20,21,23,24,25,26
category-C| 1,2,3,4,5,6,20,21,23,24,25,26 -- with gap ??
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of nutnut
nutnut
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks

declare @t table ( [from] int, [to] int)
insert into @t values ( 1, 9 )
insert into @t values ( 10, 20 )

;with data as (
  select [from], [to] from @t
   union all
   select [from] + 1, [to] from data
   where [from] < [to]
   )
, ordered as ( select top 99.999999 percent [from] from data order by [from]  )

SELECT * FROM ordered

Was enough, I just want the data going down in one column.  Thanks for help
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo