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 8626957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:09:19+00:00 2026-06-12T08:09:19+00:00

We have a requirement like fetching all the columns of a table by filtering

  • 0

We have a requirement like fetching all the columns of a table by filtering distinct values of one column along with the max value of another column.

Example:

Table A

id name qty
__ __ __

1 Dinesh 10
2 Ramesh 8
3 Dinesh 2
4 Ganesh 3
5 Ganesh 20

Here fetching distinct name along with max(qty);

Expected output:

1 Dinesh 10
2 Ramesh 8
5 Ganesh 20

Can anyone say how to achieve this in DB2 query?

Thanks in advance

  • 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-12T08:09:21+00:00Added an answer on June 12, 2026 at 8:09 am

    You could use a correlated sub-query to do this.

    SELECT T.id, T.name, T.qty
    FROM tableA T
    WHERE T.qty = (
        SELECT MAX(qty)
        FROM tableA
        WHERE name = T.name
    )
    

    However, note that if there is another record with the same name and maximum qty (such as 6 Dinesh 10) then both records will be returned. This can be resolved be writing the query slightly differently:

    SELECT T.id, T.name, T.qty
    FROM tableA T
    WHERE NOT EXISTS (
        SELECT 1
        FROM tableA
        WHERE name = T.name
            AND qty > T.qty
            OR (id > T.id AND qty = T.qty)
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In present project we have a requirement like, access one site collection column to
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
I have a requirement to combine 4 xml files into one. They are like
I have requirement like this: On one server(linux) I need to run 2 instances
I have a small requirement like . i need to fetch all record from
I have requirement like this, Have One NSIMageView and which will be changing frequently
My requirement like this: I have to fetch a row from table A. Now
Hi all I am having a requirement like selecting multiple values and then to
I have this requirement of fetching facebook user information like friends count and other
We have a requirement like this. First of all this is not a AppStore

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.