Solved

Sql query syntax

Posted on 2012-03-12
3
273 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
Comment Utility
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
Comment Utility
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
Comment Utility
good
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

771 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

11 Experts available now in Live!

Get 1:1 Help Now