?
Solved

Select multiple rows into one row, MS SQL

Posted on 2006-07-18
3
Medium Priority
?
408 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 500 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

777 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