Solved

SQL Break single rows into multiple rows

Posted on 2011-03-21
3
272 Views
Last Modified: 2012-05-11
Hi,
This should be a pretty simple task for a SQL guru. Its just a little complicated for me with the nesting.
I have a table consisting of the data I need to populate another table. However in the target table the data needs to be split into multiple rows per "item" which has a unique identifier.

I need a query that will basically take each column of the source table row and add it to a separate row in the TARGET table. I have set up a few rows of each below.The data in the TARGET table is always the same for each item id. So the query needs to create the SET of rows for EACH of the line items in the SOURCE table.
SOURCE:

[ID]    [service]     [time]    [price]
59	Golden  	30	45.00
60	Sparkle 	30	45.00

TARGET

[id]	AllowCancellations	True
[id]	AllowRescheduling	True
[id]	AppointmentDuration	[time]
[id]	AppointmentDurationMax	[time]
[id]	AppointmentInterval	[time]
[id]	CancellationFee	        0.00
[id]	DaysAhead	         30
[id]	MaxAppointments  	4
[id]	MinTimeAhead	       00:00:00
[id]	ReschedulingFee	        0.00
[id]	SchedulingFee	        [Price]

Open in new window

0
Comment
Question by:flyingsquirel
3 Comments
 
LVL 4

Assisted Solution

by:LeDaouk
LeDaouk earned 250 total points
ID: 35179151
Hi,
if you source table is not changing use the union to solve you problem

query:
Select [ID]    [service]
Union
Select [ID]     [time]  
Union
Select [ID]     [price]
...
Order By [ID]
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 total points
ID: 35179223
LIKE this create a subquery / table for the data to be added to target
and then cross join that to the source table to generate the rows to be inserted...
INSERT INTO [tARGET]
 ([ID],[nAME],[vALUE])
select s.[id],X.NAME
     ,CASE X.TYPE
           WHEN 'v' THEN X.VALUE
           WHEN 'c' 
           tHEN cASE x.vALUE 
                     WHEN 'tIME' THEN CONVERT(VARCHAR(10),S.TIME)
                     WHEN 'PRICE' THEN CONVERT(VARCHAR(20),S.PRICE)
                     END
           END        
           
 from [SOURCE] AS s
 CROSS jOIN 
(
select 'AllowCancellations' as Name,'V' as type,'True' as Value
union all select 'AllowRescheduling','V','True'
union all select 'AppointmentDuration','C','time'
union all select 'AppointmentDurationMax','C','time'
union all select 'AppointmentInterval','V','time'
union all select 'CancellationFee','V','0.00'
union all select 'DaysAhead','V','30'
union all select 'MaxAppointments','V','4'
union all select 'MinTimeAhead','V','00:00:00'
union all select 'ReschedulingFee','V','0.00'
union all select 'SchedulingFee','C','Price'
) as x

Open in new window

0
 
LVL 4

Author Closing Comment

by:flyingsquirel
ID: 35179309
Both Worked perfectly.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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