Anthony Gattuso
Anthony Gattuso

Reputation: 79

SQL Server 2008 - SELECT to combine multiple records (per ID) in to one record

I need to query the Student (STU) & Programs (PGM) tables and return one combined record per ID, even if there are multiple PGM records per ID. I thought about using GROUP BY STU.ID, STU.FN, STU.LN, but then I'm not sure how to use PGM.CD in the SELECT, since it's not an aggregate function or in the GROUP BY clause.`

The PGM table may or may not have records. My query below returns these possible results:

  1. If there are no PGM records, only one result per ID is returned and the PGM.CD column returns NULL (or '' per the CASE).
  2. If there's only one record in the PGM table, and PGM.CD = 200 ('DLA' per the CASE), then only one result per ID is returned.
  3. If there's only one record in the PGM table, and PGM.CD <> 200 ('' per the CASE), then only one result per ID is returned.
  4. If there is more than one record in the PGM table (per ID), i.e. 121, 200, 156, this produces multiple rows per ID.

I need a query to return only one row per ID, combining the results of the PGM.CD column. The PGM.CD column should only return '' or 200 ('DLA' per the CASE)

SELECT STU.ID, STU.FN, STU.LN,
  CASE PGM.CD
    WHEN '200' THEN 'DLA'
    ELSE ''
  END
FROM STU
LEFT JOIN PGM
  ON STU.ID = PGM.PID

This is what my query returns (Without the CASE modifying PGM.CD):

STU.ID  STU.FN  STU.LN  PGM.CD  
1000    Bruce   Wayne   NULL  
1001    Clark   Kent    200  
1002    Barry   Allen   151  
1002    Barry   Allen   101  
1003    Hal     Jordan  126  
1003    Hal     Jordan  200  
1003    Hal     Jordan  101

This is what my query returns (With the CASE modifying PGM.CD):

STU.ID  STU.FN  STU.LN  PGM.CD  
1000    Bruce   Wayne   
1001    Clark   Kent    DLA
1002    Barry   Allen   
1002    Barry   Allen   
1003    Hal     Jordan  
1003    Hal     Jordan  DLA
1003    Hal     Jordan  

I need it to return this:

STU.ID  STU.FN  STU.LN  PGM.CD
1000    Bruce   Wayne   
1001    Clark   Kent    DLA
1002    Barry   Allen   
1003    Hal     Jordan  DLA

I hope all of this makes sense. Thanks for the help.

Anthony

Upvotes: 1

Views: 3085

Answers (3)

mehdi lotfi
mehdi lotfi

Reputation: 11571

Use below query :

SELECT STU.ID, STU.FN, STU.LN, ISNULL(Max( Case When PGM.CD = 200 then 'DLA' Else '' End), '') as PGM_CD
FROM  STU
Left JOIN PGM ON STU.ID = PGM.PID
Group by STU.ID, STU.FN, STU.LN 

Upvotes: 0

AnandPhadke
AnandPhadke

Reputation: 13486

SELECT STU.ID, STU.FN, STU.LN,
  max(CASE PGM.CD
    WHEN '200' THEN 'DLA'
    ELSE '')
  END
FROM STU
LEFT JOIN PGM
  ON STU.ID = PGM.PID
group by  STU.ID, STU.FN, STU.LN

Upvotes: 0

Andr&#225;s Ott&#243;
Andr&#225;s Ott&#243;

Reputation: 7695

You should use a simple query like this:

   WITH CTE_PGM
    AS
    (
        SELECT PGM.PID, MAX(  CASE PGM.CD
        WHEN '200' THEN 'DLA'
        ELSE ''
        END) AS MaxCD
        FROM PGM
        GROUP BY PGM.PID
    )
    SELECT STU.ID, STU.FN, STU.LN, MaxCD
    FROM  STU
    INNER JOIN CTE_PGM ON STU.ID = CTE_PGM.PID

Try to use the SQL Fiddle(!) it is a big help in a quick answer.

I made an Example for you how is it working. Pls check it.

Upvotes: 2

Related Questions