Solved

SQL Break single rows into multiple rows

Posted on 2011-03-21
3
270 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

12 Experts available now in Live!

Get 1:1 Help Now