SQL - Update pivot table

Hi, I have two tables, the first one is like the one below:
 --------------------            ------------------
ColumnName                    Value
---------------------            -----------------
          C1                                    20
          C7                                    35
          C12                                  5


The second table has 12 columns. What I would like is to get this result from the previous table.

-----------------------------------------------------------------------------------------------------------------------
     C1          C2        C3         C4       C5        C6        C7      C8     C9     C10     C11      C12
-----------------------------------------------------------------------------------------------------------------------
      20                                                                         35                                                     5

The entries on the first table (ColumnName) can vary and can refer to any column on the second table. Is there a way to do this??

Thanks!
Adrian CrossAsked:
Who is Participating?
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.

Chris LuttrellSenior Database ArchitectCommented:
Are the 12 column names consistent?  This will have to be hard coded if they are consistent or we will have to use dynamic sql if we have to query for the names.  Also, what version of SQL are you on, want to make sure we use all the best tools available?
0
Adrian CrossAuthor Commented:
Hi, yes the 12 column names will be the same.
Im using SQL 2012
0
Scott PletcherSenior DBACommented:
You didn't specify a data type for "value", so I'll assume it's some type of numeric (int / decimal / etc.).

SELECT
    MAX(CASE WHEN ColumnName = 'C1' THEN Value END) AS C1,
    MAX(CASE WHEN ColumnName = 'C2' THEN Value END) AS C2,
    MAX(CASE WHEN ColumnName = 'C3' THEN Value END) AS C3,
    MAX(CASE WHEN ColumnName = 'C4' THEN Value END) AS C4,
    --...
    MAX(CASE WHEN ColumnName = 'C12' THEN Value END) AS C12
FROM dbo.table1
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Is the 2nd table handling only a single row? If so then you can use the following code:
INSERT INTO Table2 (C1, C2, C3, ..., C12)
SELECT SUM(C1) C1, SUM(C2) C2, SUM(C3) C3, ..., SUM(C12) C12
FROM Table1
    PIVOT (MAX(ColValue)
           FOR ColName IN ( C1, C2, C3, C4, C5, ..., C12)) P; 

Open in new window

NOTE: Replace the ... with the missing column names.
1

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.