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.
| Author |
Topic |
|
dirtyleeds
Starting Member
3 Posts |
Posted - 2008-05-08 : 01:03:30
|
| Hi,I have a table of data that looks something like: -County Name Sport----- ----- -----Kent Fred CyclingKent Fred HockeyKent Fred FootballKent Fred SquashSurrey Anne HockeySurrey Anne SquashSurrey Anne NetballSurrey Tara CyclingSurrey Tara SquashI am looking to present the data in a tabular format where I would have the data laid out something like: -County Name Cycling Hockey Football Squash Netball------ ----- ----- ------ ------ ---- -----Surrey Fred Yes Yes Yes Yes NoKent Anne No Yes No Yes Yes Tara Yes No No Yes NoWhere I group by county and then use the unique records from the sport column to dynamically create the headingsAll help greatly appreciated |
|
|
eralper
Yak Posting Veteran
66 Posts |
Posted - 2008-05-08 : 02:12:44
|
| Hello,Can you try this script.Eralper[url]http://www.kodyaz.com[/url]declare @sql nvarchar(max)select @sql = 'create table ey_report_temp (county nvarchar(10),name nvarchar(10), ' +STUFF(( SELECT distinct ',' + sport + ' nvarchar(10) ' FROM ey_report FOR XML PATH('')), 1, 1, '')+')'--select @sql exec sp_executesql @sqlselect * from ey_report_tempDECLARE @county nvarchar(max)DECLARE @name nvarchar(max)DECLARE @sport nvarchar(max)DECLARE merge_cursor CURSOR FAST_FORWARD FOR SELECT county, name, sport FROM ey_reportOPEN merge_cursor FETCH NEXT FROM merge_cursor INTO @county, @name, @sportWHILE @@FETCH_STATUS = 0BEGIN select @sql = N' update ey_report_temp set ' + @sport + ' = ''YES'' where county = ''' + @county + ''' and name = ''' + @name + '''' print @sql exec sp_executesql @sql if @@ROWCOUNT = 0 begin select @sql = N' insert into ey_report_temp ( county, name, ' + @sport + ' ) values ( ''' + @county + ''', ''' + @name + ''', ''' + @sport + ''' )' exec sp_executesql @sql end FETCH NEXT FROM merge_cursor INTO @county, @name, @sportENDCLOSE merge_cursor DEALLOCATE merge_cursor select * from ey_report_tempdrop table ey_report_temp-------------Eralperhttp://www.kodyaz.com |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2008-05-08 : 02:14:29
|
| If you are using SQL 2005, look up PIVOT, there is no need for a cursor.Although your results look incorrect for a start. |
 |
|
|
dirtyleeds
Starting Member
3 Posts |
Posted - 2008-05-08 : 05:57:53
|
| Thanks for this just trying it out and I get "Incorrect syntax near 'XML'Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-08 : 06:36:47
|
quote: Originally posted by dirtyleeds Hi,I have a table of data that looks something like: -County Name Sport----- ----- -----Kent Fred CyclingKent Fred HockeyKent Fred FootballKent Fred SquashSurrey Anne HockeySurrey Anne SquashSurrey Anne NetballSurrey Tara CyclingSurrey Tara SquashI am looking to present the data in a tabular format where I would have the data laid out something like: -County Name Cycling Hockey Football Squash Netball------ ----- ----- ------ ------ ---- -----Surrey Fred Yes Yes Yes Yes NoKent Anne No Yes No Yes Yes Tara Yes No No Yes NoWhere I group by county and then use the unique records from the sport column to dynamically create the headingsAll help greatly appreciated
Your sample data & o/p doesnt look to be in Sync. According to Sample data Fred is under Kent County but in o/p you have showed him besides Surrey? Why is that? |
 |
|
|
dirtyleeds
Starting Member
3 Posts |
Posted - 2008-05-08 : 06:47:37
|
| My mistake should have readKent Fred .......Surrey Anne .......Thanks |
 |
|
|
|
|
|
|
|