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-26 : 03:45:31
|
HII am having the table sno name subj marks1 aa tamil 901 aa english 802 bb tamil 902 bb sanskrit 1002 bb maths 100and i want the above table in the formatsno name subj1 mark1 subj2 mark2 subj3 mark31 aa tamil 90 english 80 null null2 bb tamil 100 sanskrit 100 maths 100pls tell me the query or storedprocedure for the above tasksfor the past three days,i am searching the solution for this,but didn't got yetkarthikeyan |
|
james_wells
Yak Posting Veteran
55 Posts |
Posted - 2011-11-26 : 07:43:48
|
i got the resukts in descending order1 Fred maths 100 french 100 german 100 1 me maths 80 french 60 german 20---------------------------------------------- created a temp table to match your table--------------------------------------------- declare @temptable table(smo int,name nvarchar(10),subj nvarchar(10),mark int)-------------------------------------------- populated with results------------------------------------------insert into @temptablevalues (1,'Fred','maths',100)insert into @temptablevalues (1,'Fred','french',100)insert into @temptablevalues (1,'Fred','german',100)insert into @temptablevalues (1,'me','maths',80)insert into @temptablevalues (1,'me','french',60)insert into @temptablevalues (1,'me','german',20)select smo , name , subj , markfrom @temptable------------------------------------------------------ my solution------------------------------------------------------- create temp table -----declare @temptable2 table(smo int,name nvarchar(10),comb nvarchar(1000))-- pop with group data smo / name------insert into @temptable2select smo, name, ''from @temptablegroup by smo , name--- temp cursor valuesDECLARE @smo intDECLARE @name nvarchar(10)DECLARE @subj nvarchar(10)DECLARE @mark int---- create cursordeclare getdata cursorforselect smo , name , subj , markfrom @temptableorder by smo , name , mark descopen getdataFETCH NEXT FROM getdata INTO @SMO , @NAME , @SUBJ , @MARKWHILE @@FETCH_STATUS=0BEGIN--- this bit appends the dataupdate @temptable2 set comb = comb + @subj + ' ' + cast(@mark as nvarchar(3)) + ' 'where @smo = smo and@name = nameFETCH NEXT FROM getdata INTO @SMO, @NAME , @SUBJ , @MARKENDCLOSE getdataDEALLOCATE getdata---- print resultselect * from @temptable2 |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-26 : 07:53:45
|
;with cte as(select *, seq = row_number() over (partition by sno order by marks desc, name desc)from tbl)select t1.sno, t1.name ,subj1 = t1.subj, mark1 = t1.marks , subj2 = t2.subj, mark2 = t2.marks , subj3 = t3.subj, mark3 = t3.marks , from cte t1left join cte t2on t1.sno = t2.sno and t2.seq = 2left join cte t3on t3.sno = t3.sno and t3.seq = 3where t1.seq = 1order by t1.sno If you wanted a separated string for the marks and subjects then xpath will do it in a single statement.==========================================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. |
 |
|
james_wells
Yak Posting Veteran
55 Posts |
Posted - 2011-11-26 : 19:01:33
|
Why do people put stupid remarks about other members specially when we are all trying to help i am refering to "cursors are fine if you don't know SQL" let me retortNigel there is a good reason why i did not choose to use cte as my solution especially the way you wrote it you need a join statemnet for every subject / mark combo in the source table your code does not compile - remove the last , before the fromplus if you execute the corrected code you posted and you will see that the result set is incorrect Please do not make assumption about fellow members |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-11-27 : 00:56:18
|
James, the comment about cursors is part of Nigel's auto-signature. You will see that in every one of his posts. Having seen hundreds, if not thousands of his posts, I feel confident in saying that he would not/did not intent to offend or insult you.I have not studied neither your solution nor Nigel's solution, so I am not commenting specifically about either of your postings, but you will agree that more often than not, you see cursors used in contexts where a set based solution would be far superior. In that sense, there is a grain of truth (or perhaps a bushel of truth) to Nigel's signature. |
 |
|
|
|
|
|
|