Solved

Transposing columns in to rows in SQL Server 2008

Posted on 2016-07-27
4
37 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 143

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 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