Avatar of quest_capital
quest_capital
 asked on

I need to write a SQL query that cant pull multiple records (from one table) into one record.

I need to write a SQL query that cant pull multiple records (from one table) into one record.
Example:
From:
[Table1]
first_name | records
joe | r1
joe | r2
joe | r3
to
[Table1]
first_name | records
joe | r1, r2, r3

There may be more than 3 records there could be 1 to 20 records.
Microsoft SQL ServerOracle DatabaseSQL

Avatar of undefined
Last Comment
_agx_

8/22/2022 - Mon
_agx_

There are ways to concatenate values into a single variable, but I don't think that's what you're looking for.  If you're trying to group the results for output/display in a front end application, then I recommend doing the grouping in the front end application - not in the database.  Trying to do it with sql is often more trouble and resource intensive than its worth.  
SOLUTION
Aneesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
vnvk

Solution from aneesh is the one we used many times
_agx_

@aneeshattingal,

should't you pass in the record @id instead of the name?  

@vnvk,

Yes, the coalesce trick is good. Thats what I was alluding to earlier. But I still maintain its a task for front-end applications   ;-)

I'm curious, how do you find performance? I've always found these types of functions to be a bit slow. So I tend not to use them.  What has been your experience?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Aneesh

insert into destinationTable(Name, records)
select Name, dbo.ConcatList(Name)
from yourtable
group by name

_agx_

Scratch what I said about the @id. That's obviously wrong :)
vnvk

When I used them, I found a large increase in performance compared to manipuations in the front end

SQL 2005 is more powerful than 2000 and we found increased performance in using the above technique. We tried using cursors as well as doing the changes from front, but this proved to be the best, at all times, irrespective of the size of the database or the amount of records retrieved.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
quest_capital

ASKER
aneeshattingal:
Can you do this with a select only (I can't Insert)
The code below gives me nothing. What am I doing wrong

declare @res varchar(8000)
  select @res = coalesce(@res + ',', '') + [description]
  from dbo.tmshtData
  where userid = 12345

_agx_

@vnvk,

That may be the difference.  I spend most of my time on 2000 where they seem to be slower with larger sets.  (We tried cursors too)  I would be *very* pleased if 2005 handles it better than 2000.  I think I'll revisit the idea and will do some tests next time I'm on 2005.  Thanks!
_agx_

@quest_capital,

You mean you cannot create functions?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
quest_capital

ASKER
_agx_:
no only selects this is on prodution
_agx_

Oh.  I'm not sure I know of a way to it then (at least not on 2000).  The problem is you cannot assign a @variable and select columns in the same statement.  Thats why you need the UDF (function). I assume that same restriction applies to 2005.  
ASKER CERTIFIED SOLUTION
_agx_

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.