Solved

How to split single row to multi row

Posted on 2010-09-15
4
436 Views
Last Modified: 2013-11-05
Hi Experts,
I want to split data from one row to multi rows

For example, I have a table with this columns and this data

COD   BEGIN    END
------------------------
ABC    3           5
BHG    4           7

I would like to create a query to return

COD   ITEM
--------------
ABC    3
ABC    4
ABC    5
BHG    4
BHG    5
BHG    6
BHG    7

Can you help me about this?
Thanks in advance
Leopoldo



0
Comment
Question by:onoratoInformatica
4 Comments
 
LVL 6

Expert Comment

by:johndoeherty
Comment Utility
Hi onoratoInformatica,

I'm pretty sure you will need some post processing (with php, asp, etc) after your db query.
Select your rows into a variable and iterate through the columns and reformat accordingly.

Hope this helps,

JD
0
 
LVL 4

Expert Comment

by:tabish
Comment Utility
Either you have to do what johndoeherty suggested or you'll have to write a Stored Procedure to achieve that if you want to share it at multiple front ends.
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
Comment Utility
Hi,

If you're on SQL 2005 you can try:


WITH CTE AS (
      SELECT COD, [BEGIN] AS ITEM
            FROM TABLENAME
      UNION ALL
      SELECT t.COD, C.ITEM+1 AS ITEM
            FROM TABLENAME T
                  JOIN CTE C ON C.COD = T.COD AND T.[END] > C.ITEM
)
SELECT * FROM CTE ORDER BY COD;

/peter
0
 

Author Closing Comment

by:onoratoInformatica
Comment Utility
Thanks, that's what I tried
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now