Author |
Topic |
vinothkumar.kk
Starting Member
5 Posts |
Posted - 2012-12-06 : 05:09:56
|
Hi all, I have to get column which is not in group column and join together in single column.How to achieve it in sql server 2000.ex:tablenamesec student marksA ram 20A raj 50B ramu 80B dines 99Output:sec student max(marks)A ram,raj 50B ramu,dines 99Please how to do it in sql server 2000 I need query ..It's sample data ..my wrk pjt need such like of query |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-06 : 05:20:47
|
select sec, min(student) + ',' + max(student), max(marks)from tblgroup by sec==========================================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. |
|
|
vinothkumar.kk
Starting Member
5 Posts |
Posted - 2012-12-06 : 05:30:58
|
No of student of each sec is more than 2 i just gave sample data with 2 data for each sec. |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-06 : 05:40:45
|
[code]GOCREATE FUNCTION CsvForm ( @p_sec varchar(5))RETURNS VARCHAR(4000)ASBEGIN DECLARE @a varchar(4000) = '' SELECT @a = @a + student + ',' FROM tablename WHERE sec = @p_sec RETURN LEFT(@a, LEN(@a)-1)ENDGOSELECT sec, dbo.CsvForm(sec) names, MAX(marks) maxMarkFROM tablenameGROUP BY sec[/code]--Chandu |
|
|
vinothkumar.kk
Starting Member
5 Posts |
Posted - 2012-12-06 : 05:56:15
|
Hi, My table is temporary table and hence it cant be used in procedure,functions..how to achieve it in query |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-06 : 06:33:53
|
Is there possibility to change that temporary table to normal table.. ?--Chandu |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-06 : 06:55:56
|
Does sql server 2000 include xml statements.Try the example at the top here - think you will need to change the table variable to a temp tablehttp://www.nigelrivett.net/SQLTsql/CSVStringSQL.html==========================================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. |
|
|
vinothkumar.kk
Starting Member
5 Posts |
Posted - 2012-12-06 : 07:04:10
|
Actually am in oracle now am learning sql server ..There are lot of difference in coding how to learn sql server ..It some what clumpsy |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-06 : 07:06:48
|
quote: Originally posted by nigelrivett Does sql server 2000 include xml statements.Try the example at the top here - think you will need to change the table variable to a temp tablehttp://www.nigelrivett.net/SQLTsql/CSVStringSQL.html
Hi,MSSQL 2000 supports only FOR XML RAW, EXPLICIT, AUTO but not PATH--Chandu |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-06 : 07:15:33
|
How about thiscreate table #a (sec varchar(10), student varchar(10), marks int)insert #a select 'A', 'ram', 20insert #a select 'A', 'raj', 50insert #a select 'B', 'ramu', 80insert #a select 'B', 'dines', 99insert #a select 'B', 'xxxx', 10create table #b (sec varchar(10), student varchar(1000), marks int, laststudent varchar(10))insert #b select sec, min(student), max(marks), min(student) from #a group by secwhile exists (select * from #a a join #b b on a.sec = b.sec where a.student > b.laststudent)beginupdate bset student = b.student + ',' + a.student ,laststudent = a.studentfrom #b bjoin #a aon a.sec = b.secand a.student = (select min(a2.student) from #a a2 where a2.sec = b.sec and a2.student > b.laststudent)endselect * from #b==========================================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. |
|
|
vinothkumar.kk
Starting Member
5 Posts |
Posted - 2012-12-07 : 00:51:54
|
Thanks for your help..Can you explain this queryquote: Originally posted by nigelrivett How about thiscreate table #a (sec varchar(10), student varchar(10), marks int)insert #a select 'A', 'ram', 20insert #a select 'A', 'raj', 50insert #a select 'B', 'ramu', 80insert #a select 'B', 'dines', 99insert #a select 'B', 'xxxx', 10create table #b (sec varchar(10), student varchar(1000), marks int, laststudent varchar(10))insert #b select sec, min(student), max(marks), min(student) from #a group by secwhile exists (select * from #a a join #b b on a.sec = b.sec where a.student > b.laststudent)beginupdate bset student = b.student + ',' + a.student ,laststudent = a.studentfrom #b bjoin #a aon a.sec = b.secand a.student = (select min(a2.student) from #a a2 where a2.sec = b.sec and a2.student > b.laststudent)endselect * from #b==========================================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.
|
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-12-07 : 03:20:18
|
First insert gets the distinct sec with the max mark and min student.The loop concatenates the other students associated with the sec.==========================================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. |
|
|
|