Solved

SQL Break single rows into multiple rows

Posted on 2011-03-21
3
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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