Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql query syntax

Posted on 2012-03-12
3
Medium Priority
?
282 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 1500 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 31

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

886 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