INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Congratulations on a brilliant idea and a great site..."

Geography

Where in the world do Tek-Tips members come from?
jconway (Programmer)
18 Jun 09 9:39
I am trying to concatenate the last 5 notes on each account in my database into one string field.

My DB looks like:

Acct#    Note      RowNum
1        Note 1     1
1        Note 2     2
1        Note 3     3
2        2 Note 1   1
2        2 Note 2   2
3        3 Note 1   1
4        4 Note 1   1
4        4 Note 2   2

What I need for output is:

Acct#       Notes
1           Note 1 Note 2 Note 3
2           2 Note 1 2 Note 2
3           3 Note 1
4           4 Note 1 4 Note 2

I would appreciate any suggestions on how to do this.  Thank you!
gmmastros (Programmer)
18 Jun 09 9:41
Take a look here:  thread183-1159740: condense rows to column

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

markros (Programmer)
18 Jun 09 10:03
Check links from http://forums.asp.net/p/1391544/2975969.aspx#2975969

(Took me a while to find this topic).
RyanEK (Programmer)
18 Jun 09 18:49
Hi,

If you're using SQL2005 and above you can do it in one query using xml path

CODE

declare @temp table (acct int, note varchar(10), rownum int)
insert into @temp values (1,'Note 1',1)
insert into @temp values (1,'Note 2',2)
insert into @temp values (1,'Note 3',3)
insert into @temp values (2,'2 Note 1',1)
insert into @temp values (2,'2 Note 2',2)
insert into @temp values (3,'3 Note 1',1)
insert into @temp values (4,'4 Note 2',1)
insert into @temp values (4,'4 Note 3',2)

select acct,
replace ((
  select note as 'data()'
  from @temp t2
  where t1.acct = t2.acct
  order by t2.rownum
  for xml path('')),' ',' ') as note
from @temp t1
group by acct

Ryan

Start A New Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

LINK TO THIS FORUM!
(Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum)
TITLE: Microsoft SQL Server: Programming Forum at Tek-Tips
URL: http://www.tek-tips.com/threadminder.cfm?pid=183
DESCRIPTION: Microsoft SQL Server: Programming technical support forum and mutual help system for computer professionals. Selling and recruiting forbidden.

 

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close