?
Solved

How to split single row to multi row

Posted on 2010-09-15
4
Medium Priority
?
444 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:Mike D
ID: 33680486
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
ID: 33680981
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 2000 total points
ID: 33681196
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
ID: 33681474
Thanks, that's what I tried
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

839 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