Help making SQL query

Hi Experts,

I am having some difficulty (not a SQL guy) getting a query that moves data from a source table to a destination table which is formatted differently (see attached).

 sqltables.PNG
I can write individual INSERT commands, but I'm not sure how to do a 'FOR X DO Y' query and any help to get the data from Source to Target table would be appreciated (my attempts have ended in failure).

Thanks!

(Inserting items into the ID column requires IDENTITY_INSERT to be ON)
LVL 6
Uptime Legal SystemsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
I don't think you need or want to assign the identity value yourself.  Something like below should INSERT the data.


SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
--SET IDENTITY_INSERT Target_Table ON

INSERT INTO Target_Table ( [Column], Row, Value, UUID, Customer )
SELECT ca1.[Column], st.row_num, ca1.Data, NEWID(), st.Customer
FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Customer) AS row_num
    FROM Source_Table
) AS st
CROSS APPLY (
    VALUES(1, Column1),(2, Column2), (3, Column3), (4, Column4), (5, Column5)
) AS ca1([Column], Value)
ORDER BY st.row_num, ca1.[Column]

--SET IDENTITY_INSERT Target_Table OFF

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian CroweDatabase AdministratorCommented:
DECLARE @Source TABLE
(
	Customer	VARCHAR(50),
	Column1		VARCHAR(10),
	Column2		VARCHAR(10),
	Column3		VARCHAR(10),
	Column4		VARCHAR(10),
	Column5		VARCHAR(10)
);

INSERT INTO @Source
VALUES ('Customer1', 'Data1a', 'Data1b', 'Data1c', 'Data1d', 'Data1e'),
	('Customer2', 'Data1a', 'Data1b', 'Data1c', 'Data1d', 'Data1e'),
	('Customer3', 'Data1a', 'Data1b', 'Data1c', 'Data1d', 'Data1e'),
	('Customer4', 'Data1a', 'Data1b', 'Data1c', 'Data1d', 'Data1e'),
	('Customer5', 'Data1a', 'Data1b', 'Data1c', 'Data1d', 'Data1e')

SELECT [Column], [Row], Customer, [Value], NEWID() AS UUID
FROM
(
	SELECT Customer, Column1, '1' AS Column1Label,
		Column2, '2' AS Column2Label,
		Column3, '3' AS Column3Label,
		Column4, '4' AS Column4Label,
		Column5, '5' AS Column5Label,
		ROW_NUMBER() OVER(ORDER BY Customer) AS [Row]
	FROM @Source
) AS cp
UNPIVOT
(
	[Value] FOR [Values] IN (Column1, Column2, Column3, Column4, Column5)
) AS upValue
UNPIVOT
(
	[Column] FOR [Columns] IN (Column1Label, Column2Label, Column3Label, Column4Label, Column5Label)
) AS upColumn;

Open in new window

Uptime Legal SystemsAuthor Commented:
Thanks, that puts me in the right direction.  Appreciate it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.