Solved

Sql query syntax

Posted on 2012-03-12
3
275 Views
Last Modified: 2012-03-16
Hello i have this query :


DECLARE @query NVARCHAR(4000)
SET @query = N'SELECT Parent_Key_Desc, '+
            @cols +'
            FROM
            (SELECT  t1.Parent_Key_Desc, t1.CompanyNo, t1.[tya]
            FROM   table1  AS t1) p
            PIVOT
            (
                                MAX([tya])
            FOR companyNo IN
            ( '+
            @cols +' )
            ) AS pvt
            order by Parent_Key_Desc;'



this works well.
but lets say I had one more column to pivot ? not only  MAX([tya]) but also lets say MAX([tyb])

looks like the pivot doens like I put 2 columns inside parentheses... how do we do this ?
0
Comment
Question by:PhilippeRenaud
3 Comments
 
LVL 22

Accepted Solution

by:
Nico Bontenbal earned 500 total points
ID: 37713574
I think you need to create 2 pivots. 1 for tya and 1 for tyb and then join the results.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 37723339
For one way, Nicobo's idea is correct, try it. Waiting for your comment if you need more help.
0
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 37730168
good
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Insert parts by customer 12 34
always on switch back after failover 2 35
SQL Improvement  ( Speed) 14 28
Show Results for Latest DateTime in a View 27 25
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

777 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