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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:25:34+00:00 2026-06-13T13:25:34+00:00

How do I obtain the highest value for each year within a table. So

  • 0

How do I obtain the highest value for each year within a table. So let’s say we have a table movies and I want to find the highest profiting film for each year.

This is my attempt so far:

SELECT year, MAX(income - cost) AS profit, title
FROM Movies m, Movies m2
GROUP BY year

I am pretty certain it is going to need some sub selects but I can’t visualise what I need to do. I was also thinking probably some sort of distinct option to rule out duplicate years.

Title  Year   Income  Cost  Length
A      2000   10      2     2
B      2000   9       7     2

So from this the expected result would be

Title  Year   Profit
A      2000   8
  • 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-13T13:25:35+00:00Added an answer on June 13, 2026 at 1:25 pm

    I’m guessing slightly at what you want, but since you’ve not specified any RDBMS a generic solution would be:

    SELECT  m.Year, (m.Income - m.Cost) AS Profit, m.Title
    FROM    Movies m
            INNER JOIN
            (   SELECT  m.Year, MAX(m.Income - m.Cost) AS Profit
                FROM    Movies
                GROUP BY m.Year
            ) MaxProfit
                ON MaxProfit.Year = m.Year
                AND MaxProfit.Profit = (m.Income - m.Cost)
    ORDER BY m.Year
    

    You can also do this using analytic functions if your DBMS permits. e.g. SQL-Server

    WITH MovieCTE AS
    (   SELECT  m.Year,
                Profit = (m.Income - m.Cost), 
                m.Title,
                RowNumber = ROW_NUMBER() OVER(PARTITION BY m.Year ORDER BY (m.Income - m.Cost) DESC)
        FROM    Movies
    )
    SELECT  year, Profit, Title
    FROM    MovieCTE
    WHERE   RowNumber = 1
    

    It is possible I have misunderstood your exact criteria, but I am sure the same priciples can be applied, you will just need to alter the grouping and the join in the first example, or the partition by in the second.

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

Sidebar

Related Questions

I have a table with votes from multiple sectors (think of each sector as
I want to obtain some data from dbpedia. I have entities urls and want
I want obtain full URL adres in php script, tell please, this code always
I would like to obtain, from within the Python program, the maximum physical memory
here is my problem. I have some data I binned to obtain a digitalised
Purpose: Obtain the public method signature(return value,parameter,method name) from java bytecode files. I am
1) The following query obtains from each film category the cheapest possible DVD with
I want to obtain Bar records associated to a single foo through bars in
I want to obtain last links from specific user account. I use this code:
How can I obtain a string of the Bundle Identifier programmatically from within my

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.