September 11, 2013 at 8:21 am
Hi Friends,
I have a table having some data where i want to generate sequence of rows based on premise value. I have given here the samples and the results that i am looking for..
please give me your suggestions if there is any T-SQL can do this. Any suggestions would be really appreciated. Friends, If i am not making you clear, please let me know, i can brief you.
-- My Data
select '12th Main St' as StreetName, 1 as FromPremise , 5 as ToPremise
union
select '10th Main St' as StreetName, 10 as FromPremise , 12 as ToPremise
-- Expecting Result
select '12th Main St' as StreetName, 1 as FromPremise , 1 as ToPremise
union
select '12th Main St' as StreetName, 2 as FromPremise , 2 as ToPremise
union
select '12th Main St' as StreetName, 3 as FromPremise , 3 as ToPremise
union
select '12th Main St' as StreetName, 4 as FromPremise , 4 as ToPremise
union
select '12th Main St' as StreetName, 5 as FromPremise , 5 as ToPremise
union
select '10th Main St' as StreetName, 10 as FromPremise , 10 as ToPremise
union
select '10th Main St' as StreetName, 11 as FromPremise , 11 as ToPremise
union
select '10th Main St' as StreetName, 12 as FromPremise , 12 as ToPremise
September 11, 2013 at 8:31 am
Sure you can do this pretty easily with a tally table.
;with MyData as
(
select '12th Main St' as StreetName, 1 as FromPremise , 5 as ToPremise
union
select '10th Main St' as StreetName, 10 as FromPremise , 12 as ToPremise
)
select StreetName, N as FromPremise, N as ToPremise
from MyData d
join Tally t on t.N between d.FromPremise and d.ToPremise
order by StreetName, N
You can read about tally tables here. http://www.sqlservercentral.com/articles/62867/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 11, 2013 at 9:01 am
Thank you Sean...
I was just trying with tally table..and you made me to be sure of it...Thanks a lot..
September 11, 2013 at 9:40 am
You are welcome. Glad that worked for you.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply