Solved

Select multiple rows into one row, MS SQL

Posted on 2006-07-18
3
381 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
3 Comments
 
LVL 142

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now