0

What I'm trying to do: I have records in a SQL table where there are 5 columns and thousands of rows. The rows share duplicate data (i.e. account number) but what makes each unique is that data in one of the columns is different.

As an example:

col1|col2|col3|col4|col5
------------------------
123|abc|456|def|789
123|abc|456|def|date

But the columns can have different values, not necessarily always in column 5.

Here's what I started with:

SELECT TOP (15) stuff((
            SELECT ', ' + te.[accountid]
                ,te.[char1]
                ,te.[date]
                ,te.[date2]
                ,te.[char2]
            FROM D AS te
            INNER JOIN D AS tue ON tue.[accountid] = te.[accountid]
            WHERE tue.[accountid] = ue.[accountid]
            FOR XML path('')
                ,type
            ).value('.', 'varchar(max)'), 1, 2, '') AS ifile
FROM D AS ue
GROUP BY ue.[accountid]

But I get a monster long string that includes the duplicate rows in one column. I'm not sure what else to try so any insight would be appreciated.

2
  • What is your desired output? Commented Apr 7, 2015 at 13:49
  • The desired output is: 123|abc|456|def|789|date Commented Apr 7, 2015 at 14:12

2 Answers 2

0

If I had to guess, you have an unnecessary self join in the subquery:

SELECT TOP (15) stuff((
            SELECT ', ' + te.[accountid], te.[char1], te.[date], te.[date2], te.[char2]
            FROM D te
            WHERE te.[accountid] = ue.[accountid]
            FOR XML path(''), type
           ).value('.', 'varchar(max)'), 1, 2, '') AS ifile
FROM D ue
GROUP BY ue.[accountid];

You might also want SELECT DISTINCT in the subquery.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. I went ahead and modified my query based on your suggestion and it works but it duplicates the data between the commas based on how many row entries there are per account id. Per Tab Alleman's question, my desired out is: 123|abc|456|def|789|date
0

Use UNION to get rid of all the duplicate values and use your FOR XML PATH on the output to append it to a single string:

SELECT TOP (15) stuff((
            SELECT ', ' + CAST(te.[accountid] AS varchar(255)) FROM D
            UNION 
            SELECT ', ' + CAST(te.[char1] AS varchar(255)) FROM D
            UNION
            SELECT ', ' + CAST(te.[date] AS varchar(255)) FROM D
            UNION 
            SELECT ', ' + CAST(te.[date2] AS varchar(255)) FROM D
            UNION
            SELECT ', ' + CAST(te.[char2] AS varchar(255)) FROM D
            FOR XML path('')
                ,type
            ).value('.', 'varchar(max)'), 1, 2, '') AS ifile

Untested, treat as pseudo-code to give the general idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.