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 |
karthikeyan.marlen
Starting Member
16 Posts |
Posted - 2011-11-24 : 07:37:40
|
HiI am having the table likeID Name Extracurricular1 aa karate1 aa silambamand i want to show it asID Name ExtraCurricular1 ExtraCurricular21 aa karate silambampls let me know me know the solution for thisThankskarthikeyan |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-24 : 07:58:24
|
select id, Name, ExtraCurricular1 = min(Extracurricular), ExtraCurricular2 = max(Extracurricular)from tblgroup by id, Namealways 2 rows?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-11-24 : 08:02:25
|
[code]--ID Name ExtracurricularCREATE TABLE [dbo].[Table1]([id] [int] NOT NULL,[name] [varchar](50) NULL,[extracurricular] [varchar](50) NULL)--1 aa karate--1 aa silambam--2 bb karateinsert into dbo.table1values ( 1,'aa','karate'),( 1,'aa','silambam'),( 2,'bb','karate')select * from Table1SELECT DISTINCT s1.id,s1.name, STUFF((SELECT TOP 100 PERCENT ' ' + s2.[extracurricular] FROM table1 AS s2 WHERE s2.id = s1.id ORDER BY '' + s2.[extracurricular] FOR XML PATH('')), 1, 1, '') AS [extracurricular]FROM table1 AS s1ORDER BY s1.[name] drop table Table1[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
karthikeyan.marlen
Starting Member
16 Posts |
Posted - 2011-11-24 : 08:03:01
|
No two rows always,the row nos can be any rows, if( cursor technique is the solution for this),pls tell me that solution,thanks for replying select id, Name, ExtraCurricular1 = min(Extracurricular), ExtraCurricular2 = max(Extracurricular)from tblgroup by id, Namealways 2 rows?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.[/quote]karthikeyan |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-24 : 08:12:15
|
What's wrong with the query I posted?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
karthikeyan.marlen
Starting Member
16 Posts |
Posted - 2011-11-24 : 08:18:45
|
Hi,No problem with query,It's ok when there is only two rowsif the no of rows is greater than 2,then ?for eg ,i am having the table likeID name extracurricular Marks1 aa meditation 901 aa yoga 801 aa cricket 781 aa football 67Then i want to view the above table asID Name extra1 extra2 extra3 extra4 marks1 marks2 marks3 marks41 aa medit yoga cricket foot 90 80 78 67Thanks What's wrong with the query I posted?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.[/quote]karthikeyan |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-24 : 08:53:41
|
Sorry - didn't realise what you meant by No two rows always.If you want a resultset with columns (a pivot) then I don't think it's possible without dynamic sql for an unknown number of columns.If you are happy with a seperated string for each thendeclare @table table (id int, name varchar(10), ExtraCurricular varchar(10), marks varchar(10))insert @table select 1, 'aa', 'meditation', '90'insert @table select 1, 'aa', 'yoga', '80'insert @table select 1, 'aa', 'cricket', '78'insert @table select 1, 'aa', 'football', '67'select id, name, ExtraCurricular = stuff ( ( select ';' + ExtraCurricular from @table t2 where t.id = t2.id and t.name = t2.name for xml path('') ) ,1,1,'') , marks = stuff ( ( select ';' + marks from @table t2 where t.id = t2.id and t.name = t2.name for xml path('') ) ,1,1,'')from (select distinct id, name from @table) t==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
karthikeyan.marlen
Starting Member
16 Posts |
Posted - 2011-11-24 : 09:05:01
|
Thanks for replying mebut I want the format like ID name ECA1 MARKS1 ECA2 MARKS2 ECA3 MARKS31 aa silambm 90 medit 78 cricket 64 Sorry - didn't realise what you meant by No two rows always.If you want a resultset with columns (a pivot) then I don't think it's possible without dynamic sql for an unknown number of columns.If you are happy with a seperated string for each thendeclare @table table (id int, name varchar(10), ExtraCurricular varchar(10), marks varchar(10))insert @table select 1, 'aa', 'meditation', '90'insert @table select 1, 'aa', 'yoga', '80'insert @table select 1, 'aa', 'cricket', '78'insert @table select 1, 'aa', 'football', '67'select id, name, ExtraCurricular = stuff ( ( select ';' + ExtraCurricular from @table t2 where t.id = t2.id and t.name = t2.name for xml path('') ) ,1,1,'') , marks = stuff ( ( select ';' + marks from @table t2 where t.id = t2.id and t.name = t2.name for xml path('') ) ,1,1,'')from (select distinct id, name from @table) t==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.[/quote]karthikeyan |
 |
|
|
|
|
|
|