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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:22:07+00:00 2026-06-15T01:22:07+00:00

I have a contracts table: contractId date price partId 1 20120121 10 1 2

  • 0

I have a contracts table:

contractId    date       price     partId
1             20120121   10        1
2             20110130   9         1
3             20130101   15        2
4             20110101   20        2

The contract with greatest date being the active contract (don’t blame me, I blame infor for creating xpps)

I need to create query to see only active contracts (one contract per part, the contract with highest date).

So the result of the query should be like this:

contractId    date       price     partId
1             20120121   10        1
3             20130101   15        2

I am out of ideas here, I tried self joining the table, I tried aggregation functions, but I can’t figure it out. If anyone would have any idea, please share them with me..

  • 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-15T01:22:08+00:00Added an answer on June 15, 2026 at 1:22 am

    this will work on almost all RDBMs,

    SELECT  a.*
    FROM    tableName A
            INNER JOIN
            (
                SELECT partID, MAX(date) maxDate
                FROM    tableName
                GROUP BY partID
            ) B on a.partID = b.partID AND
                    a.date = b.maxDate
    
    • SQLFiddle Demo

    if your RDBMS supports Window Function,

    SELECT  contractId ,date, price,partId
    FROM    
    (
        SELECT contractId ,date, price,partId,
                ROW_NUMBER() OVER (PARTITION BY PartID
                                    ORDER BY date DESC) rn
        FROM    tableName
    ) s
    WHERE   rn = 1
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this Event ID | Contract ID | Event date
I have 3 tables I need to join. The contracts table is the main
The case: I have 2 tables, 'contracts' and 'members' tied with contract.id = members.cid.
I have a table that represents the parameters of a contract - including their
I have a simple parent/child tables. Contract is the parent table. Each contract can
Update: Added Schema to the bottom... I have a table of contracts: tbl_contract And
I have a table with a list of contract line items in the form
In my case i have three tables. The Contracts table, the agreements table and
I have got a table where i store transactions for contracts like so: Id_Contract[int]
I have a table that stores rejected contract proposals. CREATE TABLE STATUS_CONTRATO ( STC_ID

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.