SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Group by
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vinothkumar.kk
Starting Member

India
5 Posts

Posted - 12/06/2012 :  05:09:56  Show Profile  Reply with Quote
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:
tablename
sec student marks
A ram 20
A raj 50
B ramu 80
B dines 99

Output:
sec student max(marks)
A ram,raj 50
B ramu,dines 99

Please how to do it in sql server 2000 I need query ..It's sample data ..my wrk pjt need such like of query

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/06/2012 :  05:20:47  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
select sec, min(student) + ',' + max(student), max(marks)
from tbl
group 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.
Go to Top of Page

vinothkumar.kk
Starting Member

India
5 Posts

Posted - 12/06/2012 :  05:30:58  Show Profile  Reply with Quote
No of student of each sec is more than 2 i just gave sample data with 2 data for each sec.
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 12/06/2012 :  05:40:45  Show Profile  Reply with Quote

GO
CREATE FUNCTION CsvForm ( @p_sec varchar(5))
RETURNS VARCHAR(4000)
AS
BEGIN
	DECLARE @a varchar(4000) = ''
	SELECT @a = @a + student + ',' FROM tablename WHERE sec = @p_sec
	RETURN LEFT(@a, LEN(@a)-1)
END
GO
SELECT sec, dbo.CsvForm(sec) names, MAX(marks) maxMark
FROM tablename
GROUP BY sec


--
Chandu
Go to Top of Page

vinothkumar.kk
Starting Member

India
5 Posts

Posted - 12/06/2012 :  05:56:15  Show Profile  Reply with Quote
Hi,

My table is temporary table and hence it cant be used in procedure,functions..how to achieve it in query
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 12/06/2012 :  06:33:53  Show Profile  Reply with Quote
Is there possibility to change that temporary table to normal table.. ?

--
Chandu
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/06/2012 :  06:55:56  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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 table

http://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.
Go to Top of Page

vinothkumar.kk
Starting Member

India
5 Posts

Posted - 12/06/2012 :  07:04:10  Show Profile  Reply with Quote
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
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 12/06/2012 :  07:06:48  Show Profile  Reply with Quote
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 table

http://www.nigelrivett.net/SQLTsql/CSVStringSQL.html



Hi,
MSSQL 2000 supports only FOR XML RAW, EXPLICIT, AUTO but not PATH

--
Chandu
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/06/2012 :  07:15:33  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
How about this

create table #a (sec varchar(10), student varchar(10), marks int)
insert #a select 'A', 'ram', 20
insert #a select 'A', 'raj', 50
insert #a select 'B', 'ramu', 80
insert #a select 'B', 'dines', 99
insert #a select 'B', 'xxxx', 10

create 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 sec
while exists (select * from #a a join #b b on a.sec = b.sec where a.student > b.laststudent)
begin
update b
set student = b.student + ',' + a.student ,
laststudent = a.student
from #b b
join #a a
on a.sec = b.sec
and a.student = (select min(a2.student) from #a a2 where a2.sec = b.sec and a2.student > b.laststudent)
end

select * 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.
Go to Top of Page

vinothkumar.kk
Starting Member

India
5 Posts

Posted - 12/07/2012 :  00:51:54  Show Profile  Reply with Quote
Thanks for your help..Can you explain this query

quote:
Originally posted by nigelrivett

How about this

create table #a (sec varchar(10), student varchar(10), marks int)
insert #a select 'A', 'ram', 20
insert #a select 'A', 'raj', 50
insert #a select 'B', 'ramu', 80
insert #a select 'B', 'dines', 99
insert #a select 'B', 'xxxx', 10

create 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 sec
while exists (select * from #a a join #b b on a.sec = b.sec where a.student > b.laststudent)
begin
update b
set student = b.student + ',' + a.student ,
laststudent = a.student
from #b b
join #a a
on a.sec = b.sec
and a.student = (select min(a2.student) from #a a2 where a2.sec = b.sec and a2.student > b.laststudent)
end

select * 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.

Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 12/07/2012 :  03:20:18  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000