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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:49:57+00:00 2026-05-26T07:49:57+00:00

I want to perform a simple sql query but cannot manage to get it

  • 0

I want to perform a simple sql query but cannot manage to get it right.

(Sorry for the example, that’s all I could think of to illustrate my problem)

Let’s say I have a table FooBar that contains X columns, but only 3 columns that are useful to me.

DogKind_Id, FoodQuantity, Day_Id

And I want to know, for each DogKind_Id, what was the day where the FoodQuantity was the maximum !
So for a FooBar table like :

DogKind_Id, FoodQuantity, Day_Id 
1,2, 2
1,10,1
1,4,5
2,3, 3
2,9,1
2,87,5
3,0, 3
3,0,2
3,1,6

The result of my query will be :

DogKind_Id, Day_Id
1,1
2,5
3,6

So far, I succeeded in doing this :

SELECT DogKind_Id, MAX(FoodQuantity)
FROM FooBar
GROUP BY DogKind_Id

But I cannot manage to simply use this result to get the Day_Id… If I had Day_Id in the above SELECT, it adds up lots of rows… I tried SUBSELECT, EXISTS but obviously doing it wrong !

Can you guys help me a bit ?

  • 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-05-26T07:49:58+00:00Added an answer on May 26, 2026 at 7:49 am
    SELECT parent.DogKind_id, parent.Day_Id
    FROM FooBar AS parent
    INNER JOIN (
        SELECT DogKind_Id, MAX(FoodQuantity) AS Max
        FROM FooBar
        GROUP BY DogKind_Id
    ) AS child ON (parent.DogKind_Id=child.DogKind_Id) AND (parent.FoodQuantity = child.Max)
    

    basically, the inner query is your version, which gets the id/max values.
    You then have the outer query joining against this subquery to figure out WHICH of the records has that max foodquantity, so you can get the associated dayid

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

Sidebar

Related Questions

I want to create a simple batch file that would perform some Visual Studio
It seems that every time I want to perform a db query, I have
I'm probably missing something, but I can't make a simple SQL query work. I
I want to perform simple validation against multiple fields. Please note these fields are
I want to perform cascade delete for some tables in my database, but I'm
I am creating a simple web API that returns JSON. It will perform simple
In a web project I perform a SELECT SQL query against an Indexed column
Given the following simple table structure (SQL Server 2008), I want to be able
I have a database (MS SQL) with a column that contains a query string.
I want to perform a small SQL server search in my ASP.NET web project.

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.