[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Break single rows into multiple rows

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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