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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:36:31+00:00 2026-05-26T02:36:31+00:00

I have been having difficulties with a piece of script I am creating, and

  • 0

I have been having difficulties with a piece of script I am creating, and was hoping for some insight into what I am doing wrong

QUESTION:

use a correlated subquery to return invoice(s) for each vendor, representing the vendor’s oldest invoice (the one with the earliest date). Each row should include these four columns: vendor name, invoice number, invoice date, and invoice total.

MY SCRIPT:

SELECT DISTINCT Vendor_Name,
    Invoice_number AS OLDEST_INVOICE,
    Invoice_date,
    invoice_total
FROM Vendors v
JOIN Invoices i ON v.vendor_id = i.vendor_id
WHERE invoice_date IN (
        SELECT DISTINCT MIN(invoice_date)
        FROM invoices i
        JOIN vendors v ON i.vendor_id = v.vendor_id
        GROUP BY v.vendor_name
        )
ORDER BY Invoice_Date;

Currently, my code is giving too many results back, essentially I am having trouble establishing that invoice_numbers are only to be returned for the lowest date possible.

  • 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-26T02:36:32+00:00Added an answer on May 26, 2026 at 2:36 am

    You need to restrict the join to those invoices for the specific vendor, AND that has the earliest date…
    You don;t menmtion whether the Invoice table has a surrogate PK… If it doesn’t, try:

     SELECT 
        v.Vendor_Name,  
        i.Invoice_number as OLDEST_INVOICE,  
        i.Invoice_date,  
        i.invoice_total  
     FROM Vendors v  
     JOIN Invoices i 
         ON i.vendor_id = v.vendor_id
            And i.invoice_date =
                (Select MIN(invoice_date)
                 From Invoices
                 Where vendor_id = v.Vendor_Id)  
    ORDER BY i.Invoice_Date;
    

    If the invoice table has a surrogate PK, say InvoiceId, then try this:

     SELECT 
        v.Vendor_Name,  
        i.Invoice_number as OLDEST_INVOICE,  
        i.Invoice_date,  
        i.invoice_total  
     FROM Vendors v  
        JOIN Invoices i 
           ON i.InvoiceId = 
              (Select InvoiceId
               From Invoices
               Where vendor_id = v.Vendor_Id
                  And Invoice_Date =
                       (Select Min(Invoice_Date)
                        From Invoices
                        vendor_id = v.Vendor_Id))
    ORDER BY i.Invoice_Date;
    

    The former only uses one subquery, but the latter is truer (more clearly expresses) the intent of the query’s intent.

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

Sidebar

Related Questions

I'm having some difficulties creating a sum from values I have in a collection.
We have been having some debate this week at my company as to how
I have been having problems with creating a download list of files for a
I have been struggling with this for quite some time having been accustomed to
I have been having some trouble with reminders as of lately. I was wondering
I have been having a hard time understanding how to use a singleton to
I have been having some trouble reversing my vectors in my game when a
In general, I have been having difficulties porting my .NET Windows Forms applications created
I have been having some trouble getting the following to work: <IfModule mod_rewrite.c> DirectoryIndex
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it

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.