Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flatten Rows Using FOR XML PATH 1

Status
Not open for further replies.

briangriffin

Programmer
Nov 6, 2008
878
US
Source data looks like this:

Code:
PurchaseOrderNumber   POLineNumber    TextSequence     TextLine
13472                    1                1            This
13472                    1                2            Is
13472                    1                3            Text

Found this code online and modified it, but I'm getting multiple records:

Code:
select 
    
b.PurchaseOrderID,
b.LineID,
stuff((select ',' + a.TextLine AS [text()]
                        
from 
Livendb.dbo.MmPoText a

where
a.PurchaseOrderID = b.PurchaseOrderID
and a.LineID = b.LineID
FOR XML PATH('')), 1, 1, '' )
as CombinedTest

from Livendb.dbo.MmPoText b
 
where PurchaseOrderID = 13472



Code:
PurchaseOrderNumber  POLineNumber        CombinedText
13472                    1                  This Is Text
13472                    1                  This Is Text
13472                    1                  This Is Text

Can't quite figure out how to only return one record for each PO/LineItem combination. Thanks in advance.
 
try
Code:
select b.PurchaseOrderID
     , b.LineID
     , stuff((select ',' + a.TextLine AS [text()]
              from Livendb.dbo.MmPoText a 
              where a.PurchaseOrderID = b.PurchaseOrderID
              and a.LineID = b.LineID
              FOR XML PATH('')
             ), 1, 1, '' ) as CombinedTest

from Livendb.dbo.MmPoText b
where PurchaseOrderID = 13472 
[b]group by b.PurchaseOrderID
       , b.LineID[/b]

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top