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
nutnutAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
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 ??
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok, let's split this task into 2 parts: generate the number from [from] to [to]:
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]
   )
select * 
from data
order by [from]

Open in new window


next step: put this into 1 single list. you can do this like this, for example:
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 cast([from] as varchar(10)) + ',' 
from ordered
   for xml path( '')

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nutnutAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.