Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select multiple rows into one row, MS SQL

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

610 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