Question

I've been trying to figure this out for a little bit now and i keep running into dead ends. Maybe someone here can help me. I work for a company and we are going to be receiving a file for units we are going to repair. In certain situations, we receive one claim for repair that has multiple units contained within it. I only have one field to import the serial number and I need to combine how ever many rows there is for a specific claim.

For example the file I would get would look like:

ClaimNumber       SerialNumber
555                12345
555                123456
555                1234567
556                4321
557                3421
558                9876
558                98765

So i need to export this table combining the serial numbers into one field like:

ClaimNumner       SerialNumber
555                12345, 123456, 1234567
556                4321
557                3421
558                9876, 98765

I am limited on receiving the file in this format, so this is all i have to work with. There are other fields in the file but i think these are the ones that I should be looking at. let me know if anyone has any ideas. Thanks!!

Was it helpful?

Solution

Given that your table is named Claim, then I think this should do it, (not tested)

Select Main.ClaimNumber,
   Left(Main.Serials,Len(Main.Serials)-1) As "Serials" From(Select distinct T2.ClaimNumber, 
       (Select T1.SerialNumber + ',' AS [text()]
        From Claim T1
        Where T1.ClaimNumber = T2.ClaimNumber
        ORDER BY T1.ClaimNumber
        For XML PATH ('')) [Serials]
 From Claim T2) [Main]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top