Unify 2 records in 1 - SQL SERVER 2014 [closed]

-1

Good morning,

I have 2 records that are almost identical, where they only differ in 1 column.

What I would need to do is have a single record with the fields that differ.

In the image what I have (The two registers above) and what I need (A single record with all the info of both records without repeating).

I would appreciate any idea that contributes to a solution. Thank you very much.

    
asked by Osky 02.01.2018 в 17:12
source

1 answer

1

Well, if there are always only 2 rows of data, and the different data is always in the last column, then you can use the following code:

SELECT  col1,
        col2,
        col3,
        col4,
        MIN(col5) col5,
        MAX(col5) col6
FROM dbo.TuTabla
GROUP BY col1,
         col2,
         col3,
         col4
;

Now, in this way you will not know in advance in which column the email is and in which the name. For this, you could use the following code:

SELECT  col1,
        col2,
        col3,
        col4,
        MIN(CASE WHEN col5 LIKE '%@%' THEN col5 END) col5,
        MAX(CASE WHEN col5 NOT LIKE '%@%' THEN col5 END) col6
FROM dbo.TuTabla
GROUP BY col1,
         col2,
         col3,
         col4
;
    
answered by 02.01.2018 / 17:32
source