Solved

Transposing columns in to rows in SQL Server 2008

Posted on 2016-07-27
4
29 Views
Last Modified: 2016-07-27
Please look at the screenshot and help me how I can transpose Category cost types column in to 9 columns.

transpose.JPG
I want like the resultset to be
Project Number, Project Priority List date, Secondary treatment, ...Non point source, PPL Sum

SELECT  Project.ProjectNumber, 
                   ProjectPriorityListOrderOfApprovalDate.ProjectPriorityListDate,
                    --Cat Cost Info section
                           CategoryCostType.[Type]  AS CategoryCostType,                          
                           ProjectCategoryCosts.TotalAmount AS CategoryCostTypeTotal,
                           ProjectCategoryCostInformation.PPLCategorySumAmount AS PPLSum                    
                           
                   FROM Project
                           INNER JOIN ProjectPriorityAssignmentSRFSWQIFNPS
                           on project.projectid= ProjectPriorityAssignmentSRFSWQIFNPS.projectid
                           INNER JOIN ProjectPriorityListOrderOfApprovalDate
                           on ProjectPriorityAssignmentSRFSWQIFNPS.ProjectPriorityListOrderOfApprovalDateId= ProjectPriorityListOrderOfApprovalDate.ProjectPriorityListOrderOfApprovalDateId
                            LEFT JOIN ProjectCategoryCostInformation       
                               ON ProjectCategoryCostInformation.ProjectId = Project.ProjectId                                           
                           INNER JOIN CategoryCostType
                              ON CategoryCostType.ProjectTypeId=Project.ProjectTypeId
                              LEFT JOIN ProjectCategoryCosts         
                               ON Project.ProjectId = ProjectCategoryCosts.ProjectId AND ProjectCategoryCosts.IsOrderOfApprovalCost = 1  AND 
                               ProjectCategoryCosts.CategoryCostTypeId = CategoryCostType.CategoryCostTypeId  
                  WHERE Project.projectnumber in ('3002-01','5001-01','7808-01') 
                  ORDER BY Project.ProjectNumber

Open in new window

0
Comment
Question by:Subbu G
  • 3
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 41731879
0
 

Author Comment

by:Subbu G
ID: 41731971
Thanks @angelIII

Looks like I have some syntax problem, not sure how to fix it though..can you help me fix this one?

Error:Msg 156, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'FOR'.

SELECT 'CategoryCostTypeTotal' AS CategoryCostType, 
[0], [1], [2], [3], [4],[5], [6], [7], [8], [9]
FROM
(
				  SELECT CategoryCostType.[Type]  AS CategoryCostType, ProjectCategoryCosts.TotalAmount AS CategoryCostTypeTotal
					FROM Project
                           INNER JOIN ProjectPriorityAssignmentSRFSWQIFNPS
                           on project.projectid= ProjectPriorityAssignmentSRFSWQIFNPS.projectid
                           INNER JOIN ProjectPriorityListOrderOfApprovalDate
                           on ProjectPriorityAssignmentSRFSWQIFNPS.ProjectPriorityListOrderOfApprovalDateId= ProjectPriorityListOrderOfApprovalDate.ProjectPriorityListOrderOfApprovalDateId
                            LEFT JOIN ProjectCategoryCostInformation       
                               ON ProjectCategoryCostInformation.ProjectId = Project.ProjectId                                           
                           INNER JOIN CategoryCostType
                              ON CategoryCostType.ProjectTypeId=Project.ProjectTypeId
                              LEFT JOIN ProjectCategoryCosts         
                               ON Project.ProjectId = ProjectCategoryCosts.ProjectId AND ProjectCategoryCosts.IsOrderOfApprovalCost = 1  AND 
                               ProjectCategoryCosts.CategoryCostTypeId = CategoryCostType.CategoryCostTypeId  
                  WHERE Project.projectnumber in ('5001-01') 
    ) AS SourceTable
                  
PIVOT
(CategoryCostTypeTotal FOR CategoryCostType IN ([0], [1], [2], [3], [4],[5], [6], [7], [8], [9])) AS PivotTable;

Open in new window

0
 

Author Comment

by:Subbu G
ID: 41731976
Ne ver mind,

I got it.
0
 

Author Closing Comment

by:Subbu G
ID: 41731977
Ne ver mind, I got it.


SELECT 'CategoryCostTypeTotal' AS CategoryCostType,
[0] as 'Cat1', [1], [2], [3], [4],[5], [6], [7], [8], [9]
FROM
(
                          SELECT CategoryCostType.[Type]  AS CategoryCostType, ProjectCategoryCosts.TotalAmount AS CategoryCostTypeTotal
                              FROM Project
                           INNER JOIN ProjectPriorityAssignmentSRFSWQIFNPS
                           on project.projectid= ProjectPriorityAssignmentSRFSWQIFNPS.projectid
                           INNER JOIN ProjectPriorityListOrderOfApprovalDate
                           on ProjectPriorityAssignmentSRFSWQIFNPS.ProjectPriorityListOrderOfApprovalDateId= ProjectPriorityListOrderOfApprovalDate.ProjectPriorityListOrderOfApprovalDateId
                            LEFT JOIN ProjectCategoryCostInformation      
                               ON ProjectCategoryCostInformation.ProjectId = Project.ProjectId                                          
                           INNER JOIN CategoryCostType
                              ON CategoryCostType.ProjectTypeId=Project.ProjectTypeId
                              LEFT JOIN ProjectCategoryCosts        
                               ON Project.ProjectId = ProjectCategoryCosts.ProjectId AND ProjectCategoryCosts.IsOrderOfApprovalCost = 1  AND
                               ProjectCategoryCosts.CategoryCostTypeId = CategoryCostType.CategoryCostTypeId  
                  WHERE Project.projectnumber in ('5001-01')
    ) AS SourceTable
                 
PIVOT
(avg(CategoryCostTypeTotal) FOR CategoryCostType IN ([0], [1], [2], [3], [4],[5], [6], [7], [8], [9])) AS PivotTable;
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

9 Experts available now in Live!

Get 1:1 Help Now