Solved

Sql query syntax

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MYSQL responding very slow 3 21
SQL Log size 3 17
SQL Quer 4 21
Getting invalid Syntax SQL. 3 19
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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 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