I've been using a crosstab query with MySQL which works perfectly, but now I need to implement the same thing in SQL Server 2005 and it's more difficult than I thought.There's a graphic to illustrate the table layout at http://mi6.nu/remappedsql.png and the MySQL query which works is:SELECTMAX(IF(forename='john',surname,null)) AS john, MAX(IF(forename='lucy',surname,null)) AS lucy, MAX(IF(forename='jenny',surname,null)) AS jenny,MAX(IF(forename='steve',surname,null)) AS steve,MAX(IF(forename='richard',surname,null)) as richard, addressFROM tablenameGROUP BY address;
With SQL Server, I've tried using pivot, but I can't get the query to run. This is what I have so farSELECTMAX(CASE forename WHEN 'john' THEN surname ELSE null END) AS john,MAX(CASE forename WHEN 'lucy' THEN surname ELSE null END) AS lucy,MAX(CASE forename WHEN 'jenny' THEN surname ELSE null END) AS jenny,MAX(CASE forename WHEN 'steve' THEN surname ELSE null END) AS steve,MAX(CASE forename WHEN 'richard' THEN surname ELSE null END) as richard, address FROM tablename GROUP BY address;
I'm getting errors with the MAX function though. Thanks for any help.