K.br
K.br

Reputation: 33

SQL SERVER - get data in one row

I have a table where data are stocked like this :

student| first_name   | last_name    | age     | 
------------------------------------------------
    A  |   ALEX |  NULL   |   NULL    |  NULL  |  
    A  |   NULL |  BEN    |   NULL    |  NULL  |  
    A  |   NUL  |  NULL   |   NULL    |  10    |  
    B  |   SAM  |  NULL   |   NULL    |  NULL  |  
    B  |   NULL |  NULL   |   NULL    |  15    |   

Is there a way in SQL Server to get data like this :

    student| first_name| last_name    | age     | 
    ------------------------------------------------
        A  |   ALEX    |  BEN         |   10    |   
        B  |   SAM     |  NULL        |   15    |   

Upvotes: 2

Views: 57

Answers (1)

Paul Maxwell
Paul Maxwell

Reputation: 35623

use group by, on the common column(s), and max() for columns with just one value per common column(s)

select student, max(first_name) first_name, max(last_name) last_name, max(age) age
from yourtable
group by student   

Upvotes: 3

Related Questions