?
Solved

Transposing columns in to rows in SQL Server 2008

Posted on 2016-07-27
4
Medium Priority
?
43 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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