Born2Code
Born2Code

Reputation: 137

Merge two different rows into single row in SQL with different pattern

I am trying to merge several records of same type into single row in SQL. Here is my sample data. I had also tried using COALESCE(Name + ',', '') AS Names, by merging them together and split them again in a common table. But, this is kind of merging row two records and doing an pivot on top of it.

Any inputs on this would be helpful. Thanks !

Type| Name  | Department | Value1 |  Value 2 | Value 3 
-----------------------------------------------------
1  | John  |  A         |  100   |  NULL    |  NULL
1  | John  |  B         |   NULL |  200     |  NULL
1  | John  |  C         |  NULL  |  NULL    |  300
2  | Kay   |  A         |  400   |  NULL    |  NULL
2  | Kay   |  B         |  NULL  |  500     |  NULL
3  | Lor   |  B         |  NULL  |  600     |  NULL

Edited: Apologies for not properly articulating my original problem. Updated below again.
Here is the transformation output looking after the query. Given departments can be many and they are transformed to only 3 columns in the output based on Dept Type.

ID | Name  | Department1 | Department 2| Department 3 | Value1 |  Value 2 | Value 3 
-----------------------------------------------------
1  | John  |  A         |          B   |      C       |  100   |  200    |  300
2  | Kay   |  A         |          B   |      NULL    |  400   |  500    |  NULL
3  | Lor   |  NULL      |          B   |       NULL   |  NULL  |  600    |  NULL

I was trying with below query, but i am not able to transform department values into column values. 

SELECT 
   ID, 
   MAX(Value1) AS Value1, 
   MAX(Value2) AS Value2, 
   MAX(Value3) AS Value3 
FROM Employee
GROUP BY Name ```> 

Upvotes: 0

Views: 59

Answers (1)

Cetin Basoz
Cetin Basoz

Reputation: 23837

You could do that as:

SELECT 
   ID, 
   Name,
   Max(case when department = 'A' then 'A' end) as Department1,
   Max(case when department = 'B' then 'B' end) as Department2,
   Max(case when department = 'C' then 'C' end) as Department3,
   MAX(Value1) AS Value1, 
   MAX(Value2) AS Value2, 
   MAX(Value3) AS Value3 
FROM Employee
GROUP BY Id, Name;

Thinking it is just for some kind of pivot like display.

UPDATE: I am not really sure if you meant as in your sample though (would be nice if your question is clear):

WITH
  x AS (
         SELECT
              Name, Department, DENSE_RANK() OVER (PARTITION BY Name ORDER BY Department) AS dept
         FROM Employee
       )
SELECT
         t.Type                                        AS ID
       , t.Name
       , MAX(CASE WHEN x.dept=1 THEN x.Department END) AS Department1
       , MAX(CASE WHEN x.dept=2 THEN x.Department END) AS Department2
       , MAX(CASE WHEN x.dept=3 THEN x.Department END) AS Department3
       , MAX(Value1)                                   AS Value1
       , MAX(Value2)                                   AS Value2
       , MAX(Value3)                                   AS Value3
FROM     Employee t
         INNER JOIN x ON x.Name=t.Name
GROUP BY t.Type, t.Name
ORDER BY t.Name;

DBFiddle demo is here

Upvotes: 2

Related Questions