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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:23:33+00:00 2026-06-03T21:23:33+00:00

I am a bit new to SQL on the whole and I managed to

  • 0

I am a bit new to SQL on the whole and I managed to stumble my way through this far but now I am a bit stuck.

I have two tables one is an index of “Ships” to “Oil Types” just Id’s. The other table “ROB” is the amount of oil remaining on the ship.

Each month I may or may not get a new record in the ROB table.

What I need is each distinct oil type for a cetain ship and the latest ROB if there is one.

The following query nets me almost what I want, except I get all ROB’s for each oil type and I want only the latest.

PARAMETERS QueryShipID Short;
SELECT Oils.OilID, Oils.ShipID, ROB.LastROB, ROB.Received, ROB.DateReceived, ROB.PortReceived, ROB.TotalUsed, ROB.CurrentROB, ROB.DateSent
FROM 
    (SELECT DISTINCT OilID, ShipID 
    FROM [Index] 
    WHERE (((ShipID)=[QueryShipID])))  
    AS Oils 

    LEFT JOIN 

    (SELECT ShipID, OilId, LastROB, Received, DateReceived, PortReceived, TotalUsed, CurrentROB, DateSent 
    FROM [Oil ROB])  
    AS ROB 

    ON (Oils.ShipID = ROB.ShipID) AND (Oils.OilID = ROB.OilID);

What I need to do is get the last DateSent for each oil type.

I have found a few tutorials on using the Max function which seem to suggest joining the results of an aggregate query back to the orginal table. However I can’t seem to get this to work and it makes a huge and messy query.

If anyone has some tips on the best way to do this I would be very grateful.

Ideally after doing this I would like to add a parameter for date so you can get the last ROB before a certain date. This should be relatively easy but I include it in case it has any bearing on the answers.

Thanks you.

Dave

  • 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-03T21:23:35+00:00Added an answer on June 3, 2026 at 9:23 pm

    Thanks nawful

    I am posting the answer myself as it is a slight modification of what nawful posted. Also the link K’leg posted was helpful so thanks.

    This gets me exactly what I want…

    SELECT    Oils.OilID, Oils.ShipID, ROB.LastROB, ROB.Received, ROB.DateReceived,  
              ROB.PortReceived, ROB.TotalUsed, ROB.CurrentROB, ROB.DateSent 
    FROM  
              ( 
               SELECT DISTINCT OilID, ShipID  
               FROM   [Index]  
               WHERE  ShipID = [QueryShipID] 
              ) AS Oils             
    LEFT JOIN   
              ( 
               SELECT ShipID, OilId, LastROB, Received, DateReceived, PortReceived,  
                      TotalUsed, CurrentROB, DateSent  
               FROM   [Oil ROB]  
              )  AS ROB ON Oils.ShipID = ROB.ShipID AND Oils.OilID = ROB.OilID
    WHERE     ROB.DateSent = ( 
                              SELECT MAX(DateSent)  
                              FROM   [Oil ROB]  
                              WHERE  ShipID = ROB.ShipID AND  
                                     OilID = ROB.OilID
                             );
    

    The main modification is that I need the OilID from [Oil ROB] table to match the ROB.OilID not Oils.OilID. Also I moved the WHERE to the outermost SELECT.

    Couldn’t have done it without you though 🙂

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

Sidebar

Related Questions

I'm a bit new to SQL and have trouble constructing a select statement. I
I am a bit new to the Devel::Cover module, but have found it very
I am a bit new with android but I have some java experience. So
I am a bit new to reusable plugins for jquery. I have ran across
im a bit new at extension development, but im about to embark upon a
Hey guys, I have this quick bit of code that I can't figure out
In Sql Server 2005, I have two databases. In the first one I have
I've had this problem a few times and solved it the same way. But
Hi everyone I am a bit new to PL/SQL so am a bit confused.
I have a question which might sound a bit confusing but i'll try 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.