简体   繁体   中英

SQL - Combine Multiple Columns with Multiple Rows into one row

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 (ie 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM