Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concatenating fields into one


Concatenating fields into one

Author
Message
richardmgreen1
richardmgreen1
Right there with Babe
Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)

Group: General Forum Members
Points: 786 Visits: 893
Hi all

I'm running SQL 2005 with SSMS 2012.
I've got a table with the following fields:
Visit ID
Abstract ID
QuerySeqID (Query Sequence ID)
Response


The Visit ID and Abstract are unique to a patient/visit, the QuerySeqID increases by 1 up to a possibly infinite number (the highest I've got so far is 18).
The Response field conains the notes I need to concatenate.

I've tried the follwoing code:-

;with cte as
(select
*
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult1
where
mult1.[Query]='Additional Notes'
)

select
c.VisitID
,c.AbstractID
,c.QuerySeqID
,c.Response+c1.Response
from
cte c
left outer join cte c1
on c.VisitID=c1.VisitID
and c.AbstractID=c1.AbstractID
and c1.QuerySeqID=c.QuerySeqID+1
where
c1.response is not null
order by
c.VisitID
,c.AbstractID
,c.QuerySeqID


and it's getting me close, but......
If there's a record with 3 lots of notes, I'm getting the following:
Note1 + Note2
Note2 + Note3

What I want to get to is:-
Note1 + Note2 + Note3 + ....... Note(n)

I don't think I'm far off with what I'm doing but I can't seem to get everything into one record.

Would someone be kind enough to have a look and see where I'm going wrong/where the code needs a tweak?

I'd post some data but it's sensitive.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47241 Visits: 44377
Have a look at this (the XML method mainly): https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


richardmgreen1
richardmgreen1
Right there with Babe
Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)

Group: General Forum Members
Points: 786 Visits: 893
Thanks for that Gail
I've brought back one set of records using each method and this is what I'm getting for the Response field....
My CTE:-
4 Cant change status to final
Cant change status to final Procedure needs removing

The XML Path method:-
UNFIN,4 ,CANT CHANGE STATUS TO FINAL ,PROCEDURES NEED REMOVING,


I can remove the final comma easily enough, but I'm not sure where the UNFIN and the "&xOD;" bit's are coming from (I'm assuming "&xOD;" is a carriage returns, line feed or similar).

Any ideas?


::edit::
Forgot to include my updated code:-

select
mult1.VisitID
,mult1.AbstractID
,(select
Response + ','
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult2
where
mult2.VisitID=mult1.VisitID
and mult2.AbstractID=mult1.AbstractID
order by
mult2.VisitID
,mult2.AbstractID
for xml path ('')) as overall
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult1
where
mult1.VisitID='RA0-20120603025910735'
and mult1.[Query]='Additional Notes'

group by
mult1.VisitID
,mult1.AbstractID


GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47241 Visits: 44377
The 0x... Could be XML encoding. REPLACEs are often necessary to fix that. The unfin not coming from the data? Check filters.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


richardmgreen1
richardmgreen1
Right there with Babe
Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)

Group: General Forum Members
Points: 786 Visits: 893
Thanks Gail.
I've written the REPLACE to get rid of the x0D's and that bit's worked.

You were right about the UNFIN, it was picking up a previous field that I hadn't filtered out in the sub-select query.

Thanks for all your help.
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6714 Visits: 17699
Quick suggestion, add the .value() method to get the correct output
Cool
select
mult1.VisitID
,mult1.AbstractID
,(select
Response + ','
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult2
where
mult2.VisitID=mult1.VisitID
and mult2.AbstractID=mult1.AbstractID
order by
mult2.VisitID
,mult2.AbstractID
for xml path (''), TYPE).value('.[1]','NVARCHAR(MAX)') as overall
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult1
where
mult1.VisitID='RA0-20120603025910735'
and mult1.[Query]='Additional Notes'

group by
mult1.VisitID
,mult1.AbstractID


richardmgreen1
richardmgreen1
Right there with Babe
Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)Right there with Babe (786 reputation)

Group: General Forum Members
Points: 786 Visits: 893
Hi again

The powers that be have now decided they only need the first 300 characters from the XML-based code I'm currently using.
I've left the code as-is and I'm using

left([Additional Notes].Comments,300)


to get the bit I need.
Is there any way of restricting the code below to just return 300 characters (hopefully it will speed things up as well):-

select
mult1.VisitID
,mult1.AbstractID
,replace(replace(replace(replace((
select
Response + ','
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult2
where
mult2.VisitID=mult1.VisitID
and mult2.AbstractID=mult1.AbstractID
and mult2.[Query]='Additional Notes'
order by
mult2.VisitID
,mult2.AbstractID
for xml path ('')),' ',''),'&','&'),char(13),''),char(10),'') as Comments
from
[livedb_daily].[dbo].[AbsProjectsQueriesMultCs] mult1
where
mult1.[Query]='Additional Notes'
group by
mult1.VisitID
,mult1.AbstractID) [Additional Notes]



It doesn't take too long (about 20 minutes) to sort out just over 1,000,000 records (there are quite a few joins) but a saving is a saving.

I've already enquired about adding indexes (there aren't any :angrySmile and that should speed things up as well.
I can't add the indexes directly as this is a third-part app that writes to a SQL database.

Any pointers gratefully received).

As an aside, I don't quite understand how the FOR XML PATH thing works and the link didn't really explain it (nor does any site I've found courtesy of Google). Would someone kindly explain?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search