Solved

Select multiple rows into one row, MS SQL

Posted on 2006-07-18
3
401 Views
Last Modified: 2008-01-09
I have two tables A,B having structure in Sql server 2000
A- studentID int, studentName varchar(200)
having records
      1 a1
      2 a2
      3 a3

B- studentID int attribute varchar(50)
      1 x
      1 y
      1 z
      1 a
      1 b
      1 c
      2 p
      2 q
      2 r
      3 m
      3 n
      3 o
Now when i run query
Select studentID,StudentName,attribute from A,B where a.studentID= b.studentID
Gives

1 a1 x
1 a1 y
1 a1 z
...........
.........
Is it possible in any way that i get records like
1 a1 x y z a b c
2 a2 p q r
3 a3 m n o


0
Comment
Question by:softsolvers
[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
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17129727
create function dbo.concatvalues(@studentid int)
returns varchar(2000)
as
begin
  declare @res varchar(2000)
  select @res = coalesce( @res + ',' , '') + attribute
   from B where studentid = @studentid
  return @res
end

and use it like this:
select a.*, dbo.concatvalues(a.studentid) as attributes
from A
   
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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