Home > Software design >  SQL - combining two rows of data into one with a common identifier
SQL - combining two rows of data into one with a common identifier

Time:08-04

I am working on a project where I have to solve the following problem.

Goal:

  1. If there are two rows that same the same identifier, but additional data that is different, how can I combine all of that data into one row with individual columns?

Example:

DateBase:

| ID   | Rating | Rating Provider|
--------------------------------
| 5055 | A      | Moodys         | 
---------------------------------
| 5055 | Bb     | SNP            |

Desired End Result:

| ID   | Moodys | SNP | 
--------------------
| 5005 | A      | Bb  | 

CodePudding user response:

I believe you simply need a Pivot -

SELECT *
  FROM YOUR_TABLE
 PIVOT(MAX(Rating)
       FOR Rating_Provider IN (Moodys AS 'Moodys', SNP AS 'SNP'));

CodePudding user response:

Quantnesto, i believe that what you are looking for it's the JOIN function. You have the information in different databases, right?

  1. You SELECT all the fields that you want from the different tables
SELECT a.ID,a.Moodys,B.SNP
FROM DataBase a
JOIN Database b on a.ID = b.ID

And that's it.

There are different kinds of JOIN's, for further information let me know, i can explain each type.

  • Related