Solved

Sql query syntax

Posted on 2012-03-12
3
274 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now