Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:14:19+00:00 2026-06-10T11:14:19+00:00

I need to query the Student ( STU ) & Programs ( PGM )

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:14:20+00:00Added an answer on June 10, 2026 at 11:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to query one object with its related objects, I am using the
I need a query that will return a table where each column is the
I have 2 tables, Student and Supervisor: STUDENT (supervisorid(pk),name,email....) SUPERVISOR (supervisorid(pk),name,email....) Now I need
I need thinking_sphinx query to retrieve begins_with values. means if I give Student.search 'a',
Here are the tables: I need to retrieve a collection of Student objects, so
I have two tables. Student and Job. I need to search students who possess
I have a database where I need to query to get records from one
I need query to get data between two days.I tried with this.But it gives
Need to query my SQL server from Access using an ADO connection (for example),
I need to query statistics about duplicate values in my database table. For example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.