Solved

In SQL Server 2000, how do I write sql to display grouped data in one row (Similar to how the Stuff function works in 05)

Posted on 2009-07-15
6
276 Views
Last Modified: 2012-05-07
I have a sql statement which groups on department and returns:

Accounting    Ed
Accounting    Mike
Accounting    Peter
Sales    Janet
Sales    Susan

My question is, is it possible in sql (sql server 2000) to return the following:

Accounting    Ed,Mike,Peter
Sales    Janet,Susan


It doesnt have to be seperated by a comma, i just need to know what the technique is to return all the names on the same line as the department.

Here is what the SQL is:

Select Department, Name
From Records
group by Department, Name
0
Comment
Question by:BostonMA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 24860116
Yes.. Its possible..
Just follow this example which I did before long time to get it done
--Sample table schema
Create table SchemaID
(
[ID] smallint,
SchemaID int NOT NULL
)
go
 
--Dummy insert statements
Insert into SchemaID values (1,12)
Insert into SchemaID values (1,13)
Insert into SchemaID values (1,14)
 
Insert into SchemaID values (2,15)
Insert into SchemaID values (2,16)
Insert into SchemaID values (2,17)
Insert into SchemaID values (2,18)
 
CREATE TABLE #temp_result ( a varchar(20), b varchar(1000));
 
Declare @id_con varchar(100);
DECLARE @id_in varchar(100);
DECLARE loop_val CURSOR FOR
SELECT DISTINCT cast(id AS varchar(20))
FROM SchemaID;
 
OPEN loop_val;
 
FETCH NEXT FROM loop_val
INTO @id_in;
 
WHILE @@FETCH_STATUS = 0
BEGIN
Select @id_con=Coalesce(@id_con + ', ', '') + Cast(SchemaID AS varchar(5)) From SchemaID Where [ID] = @id_in
INSERT INTO #temp_result VALUES (@id_in, @id_con);
SET @id_con = NULL;
FETCH NEXT FROM loop_val
INTO @id_in;
END
 
CLOSE loop_val;
DEALLOCATE loop_val;
 
SELECT * FROM #temp_result
 
-- Drop table once you fetch your Results
DROP TABLE #temp_result

Open in new window

0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24860125
ID in my example is your Department and Name combination.
Just try yourself once with the code sample and if you face any errors, ready to help you...
0
 
LVL 4

Author Comment

by:BostonMA
ID: 24860593
I get the idea of your solution, and i could probably make it work given enough time, however at the present I dont have security access to create a table. Do you know of any other ways which dont involve creating a temp table?  

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24861415
Then I would suggest you to go for table variable which is not available in 2000.
Hence no other approach strikes in my mind for 2000 other than temp table.
0
 
LVL 4

Author Closing Comment

by:BostonMA
ID: 31603768
Thanks.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24865578
Welcome and glad to help you out..
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question