[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Transposing columns in to rows in SQL Server 2008

Posted on 2016-07-27
4
Medium Priority
?
46 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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