Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Pivot Function

Author  Topic 

JohnGault777
Starting Member

31 Posts

Posted - 2011-09-28 : 14:49:06
I have this query:

  SELECT convert(varchar,SSG_Rate.dbo.AutoYearMakeModel_Factor.vehicleYear,4) + '_' + SSG_Rate.dbo.AutoYearMakeModel_Factor.make + '_' + 
SSG_Rate.dbo.AutoYearMakeModel_Factor.model as 'New_ID',
ssg_rate.dbo.AutoYearMakeModel_Factor.coverageTypeID, ssg_rate.dbo.AutoYearMakeModel_Factor.factor, SSG_Rate.dbo.CoverageType.type
FROM ssg_rate.dbo.AutoYearMakeModel_Factor inner join [SSG_Rate].[dbo].[CoverageType] on
SSG_Rate.dbo.AutoYearMakeModel_Factor.coverageTypeID = SSG_Rate.dbo.CoverageType.coverageTypeID


It generates these results (sorry, I don't know how to make them line up in columns):

New_ID coverageTypeID ratingversionid factor type
2009_ACURA_TSX 10 10 0.95 BI
2009_ACURA_TSX 11 10 0.95 PD
2009_ACURA_TSX 18 10 2.08 COLL
2009_ACURA_TSX 19 10 2.91 COMP
2009_ACURA_TSX 23 10 2.91 LOAN
2009_ACURA_TSX 17 10 1.07 PIP
2009_ACURA_TSX 12 10 1.07 UMBI
2009_ACURA_TSX 20 10 1.07 MED
2010_MAZDA_2 26 12 1.04 BI
2010_MAZDA_2 42 15 1.04 BI
2010_MAZDA_2 34 12 1.10 COLL
2010_MAZDA_2 50 15 1.10 COLL
2010_MAZDA_2 35 12 0.69 COMP
2010_MAZDA_2 51 15 0.69 COMP
2010_MAZDA_2 39 12 0.69 LOAN
2010_MAZDA_2 55 15 0.69 LOAN
2010_MAZDA_2 33 12 0.83 PIP
2010_MAZDA_2 44 15 0.83 UMBI
2010_MAZDA_2 28 12 0.83 UMBI
2010_MAZDA_2 36 12 0.83 MED
2010_MAZDA_2 52 15 0.83 MED
2010_MAZDA_2 49 15 0.83 PIP
2009_HYUNDAI_GENESIS 3.8 26 12 0.98 BI
2009_HYUNDAI_GENESIS 3.8 27 12 0.98 PD
2009_HYUNDAI_GENESIS 3.8 34 12 1.96 COLL

I want to be able to pivot the table so that it looks like this:

BI PD COLL COMP LOAN PIP UMBI MED
2009_ACURA_TSX 0.95 0.95 2.08 2.91 2.91 1.07 1.07 1.07
2010_MAZDA_2
2009_HYUNDAI_GENESIS 3.8

But it needs to be where ratingversionid is greatest.

How do I write the query?




JG777

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-28 : 15:00:13
why 2010_MAZDA_2 has no values?

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

Go to Top of Page

JohnGault777
Starting Member

31 Posts

Posted - 2011-09-28 : 15:02:37
I just didn't go through the process of matching the actual data to the desired result example. Sorry, imagine that there are figures next to those two rows.

JG777
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-28 : 15:09:15
ok then it should be like:-


SELECT New_ID,
MAX(CASE WHEN type='BI' THEN factor ELSE NULL END) AS BI,
MAX(CASE WHEN type='PD' THEN factor ELSE NULL END) AS PD,
MAX(CASE WHEN type='COLL' THEN factor ELSE NULL END) AS COLL,
MAX(CASE WHEN type='COMP' THEN factor ELSE NULL END) AS COMP,
MAX(CASE WHEN type='LOAN' THEN factor ELSE NULL END) AS LOAN,
MAX(CASE WHEN type='PIP' THEN factor ELSE NULL END) AS PIP,
MAX(CASE WHEN type='UMBI' THEN factor ELSE NULL END) AS UMBI,
MAX(CASE WHEN type='MED' THEN factor ELSE NULL END) AS MED
FROM
(
SELECT DENSE_RANK() OVER (PARTITION BY New_ID ORDER BY ratingversionid DESC) AS rn,
New_ID, coverageTypeID, ratingversionid, factor, type
FROM table
)t
WHERE rn=1
GROUP BY New_ID


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

Go to Top of Page

JohnGault777
Starting Member

31 Posts

Posted - 2011-09-28 : 16:48:00
Perfection. Thank you. That was exactly what I needed. You just saved me a lot of time.

JG777
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-28 : 22:54:55
welcome

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

Go to Top of Page
   

- Advertisement -