?
Solved

SQL Break single rows into multiple rows

Posted on 2011-03-21
3
Medium Priority
?
275 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 1000 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 1000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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