Solved

Transposing columns in to rows in SQL Server 2008

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Job - date manual 1 47
Are triggers slow? 7 28
Trying to understand why my Index is so large 12 52
SQL 2008 - Disappearing Temp Table in Stored Procedure 24 58
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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