SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 PIVOT and Crosstab difficulties
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SCHEMA
Posting Yak Master

192 Posts

Posted - 04/22/2012 :  10:28:22  Show Profile  Reply with Quote
quote:
Originally posted by SCHEMA

Here is my how table looks like


DisplayValue	        RID	                              Seq	Code   ColumnName	       Value
DegreeProgram	       AF48C244-8693-4FD4-9D01-AF5794CFD458	1	13	Column1	                20
Role	               AF48C244-8693-4FD4-9D01-AF5794CFD458	1	13	Column2	                 5
PopulationFoci	       AF48C244-8693-4FD4-9D01-AF5794CFD458	1	13	Column3	
DegreeProgram	       AF48C244-8693-4FD4-9D01-AF5794CFD458	2	13	Column1	                 10
Role	               AF48C244-8693-4FD4-9D01-AF5794CFD458	2	13	Column2	                 15
PopulationFoci	       AF48C244-8693-4FD4-9D01-AF5794CFD458	2	13	Column3	                 20
DegreeProgram	       AF48C244-8693-4FD4-9D01-AF5794CFD458	3	13	Column1	                 20
Role	               AF48C244-8693-4FD4-9D01-AF5794CFD458	3	13	Column2	                 5
PopulationFoci	       AF48C244-8693-4FD4-9D01-AF5794CFD458	3	13	Column3	
DegreeProgram	       71232BEC-3723-4B89-B626-657F14DD5426	1	13	Column1	                 15
Role	               71232BEC-3723-4B89-B626-657F14DD5426	1	13	Column2	                 20
PopulationFoci	       71232BEC-3723-4B89-B626-657F14DD5426	1	13	Column3	



Here is my desired output


RID	                               Code	Seq	DegreeProgram	Role	PopulationFoci
71232BEC-3723-4B89-B626-657F14DD5426	13	1	15	20	
AF48C244-8693-4FD4-9D01-AF5794CFD458	13	1	20	5	
AF48C244-8693-4FD4-9D01-AF5794CFD458	13	2	10	15	20
AF48C244-8693-4FD4-9D01-AF5794CFD458	13	3	20	5





Simply I can use this query and get result
Select RID,Code,Seq,
MAX(Case When ColumnName = 'Column1' then Value Else null End) as [DegreeProgram],
MAX(Case When ColumnName = 'Column2' then Value Else null End) as [Role],
MAX(Case When ColumnName = 'Column3' then Value Else null End) as [PopulationFoci]
from bpmh.dbo.tmp_PVT
Group by RID,Code,Seq
Order by 1


But How to achieve the same with PIVOT Query? I am using PIVOT dynamically. Please help

Edited by - SCHEMA on 04/22/2012 10:45:36

visakh16
Very Important crosS Applying yaK Herder

India
48064 Posts

Posted - 04/22/2012 :  13:13:36  Show Profile  Reply with Quote

SELECT RID,Code,Seq,[Column1],[Column2],[Column3]
FROM
(
SELECT RID,Code,Seq,ColumnName,Value
FROM bpmh.dbo.tmp_PVT
)t
PIVOT ( MAX([Value]) FOR ColumnName IN ([Column1],[Column2],[Column3]))p
ORDER BY RID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000