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)
 Rows to columns

Author  Topic 

Mr Butter
Starting Member

3 Posts

Posted - 2011-10-19 : 22:06:07
I have a table called s1 that has participants with social security numbers (SSN),name (NM), and a case number (CN). The case number can have more than one participant.

ssn NM CN
123 jim 100
124 Steve 2oo
125 Sam 100

I want the results to equal the following:
cn nm nm1
100 jim sam
200 Steve (null)

Please excuse the simplicity of this request. Thank you M.Butter

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-20 : 00:36:55
[code]
SELECT CN,
MAX(CASE WHEN rn=1 THEN NM ELSE NULL END) AS NM1,
MAX(CASE WHEN rn=2 THEN NM ELSE NULL END) AS NM2
FROM (select row_number() over (partition by CN order by ssn) as rn,* from table)t
GROUP BY CN
[/code]

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

Go to Top of Page
   

- Advertisement -