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

  • Home
  • SEARCH
  • 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 8813825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:57:24+00:00 2026-06-14T03:57:24+00:00

This seems like a really simple problem but I can’t seem to figure it

  • 0

This seems like a really simple problem but I can’t seem to figure it out right now…

Here is a simplified view of the data that I am fetching from my current stored proc:

ID     Name         Class     Desc
---    -----        ------    -----
84     Calvin J.    2B
53     Fred D.      3B
53     Fred D.      ADJ       Change/Correction
47     Mary F.      3A
47     Mary F.      ADJ       New Product
09     Donald M.    ADJ       Cancelled
21     Richard G.   ADJ       Bad Debt
21     Richard G.   ADJ       Cancelled

I need to modify my procedure to select only one record per individual. If a person has an adjustment, I only want to select the record with the adjustment and disregard the other record. Based on the above, this is the result set that I am trying to return:

ID     Name         Class     Desc
---    -----        ------    -----
84     Calvin J.    2B
53     Fred D.      ADJ       Change/Correction
47     Mary F.      ADJ       New Product
09     Donald M.    ADJ       Cancelled
21     Richard G.   ADJ       Cancelled

Help please!

UPDATE

I just realized that there is an additional requirement for this query; if there are two adjustments where one has a description of “Bad Debt” and the other “Cancelled”, the record with the “Cancelled” description needs to be selected (see updated data above).

  • 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-14T03:57:25+00:00Added an answer on June 14, 2026 at 3:57 am

    This should do the trick:

    SELECT ID, Name, Class, [Desc]
    FROM (
      SELECT ID, Name, Class, [Desc],
             ROW_NUMBER() OVER(PARTITION BY ID
                               ORDER BY CASE WHEN Class = 'ADJ'
                                             THEN 0 ELSE 1 END) rn
      FROM Table1
    ) A
    WHERE rn = 1
    

    It looks scarier than it really is. The inner query contains an extra column computed with ROW_NUMBER(). What this does is number your rows, starting over at 1 for each distinct ID (specified in the PARTITION BY). The ORDER BY, which tells ROW_NUMBER() how to order the rows, is a case statement saying that rows with Class = 'ADJ' should come before all other rows. Then at the end we grab only rows numbered 1. The result is selecting the ADJ row if there is one for that ID, or the regular row otherwise.

    Edit in response to updated requirements
    If you have additional prioritization criteria then you can add those into the ORDER BY, just like you would ORDER BY in a regular query. Often it’s helpful to execute just the inner query without filtering down to rn = 1 so you can see exactly how row numbers are being assigned.

    Here’s the updated query that should satisfy your new requirement:

    SELECT ID, Name, Class, [Desc]
    FROM (
      SELECT ID, Name, Class, [Desc],
             ROW_NUMBER() OVER(PARTITION BY ID
                               ORDER BY
                                 CASE WHEN Class = 'ADJ'
                                      THEN 0 ELSE 1 END,
                                 CASE WHEN [Desc] = 'Cancelled'
                                      THEN 0 ELSE 1 END) rn
      FROM Table1
    ) A
    WHERE rn = 1
    

    See it in action here.

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

Sidebar

Related Questions

This seems like a really, really simple problem, but I can't figure it out.
This seems like it should be really simple, but I'm unable to figure this
Ok, so this seems like a really silly problem but I can't get my
This really seems like a bug to me, but perhaps some databinding gurus can
I'm having a slight problem that I can't figure out, but should be really
My problem seems to be really simple but I just can't get the reason
This seems like it should be really easy but I couln't find an answer
This seems like such a simple issue but I cannot find an elegant solution.
This seems like the simplest Git question, but I can't find ANYTHING on it.
This seems like it should be relatively straightforward, I cannot figure out what I'm

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.