Results 1 to 4 of 4

Thread: SQL Query How to?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Sudbury, Ontario, Canada
    Posts
    274
    I have a database that tracks client invoices with a one to many relationship. What I want to do is pull the last invoice for a client based on the invoice date.

    Such as:
    Code:
    strsql = "SELECT Nrcs.file_num, MAX(invoice.date), "
    strsql = strsql & "Invoice.amount, Invoice.invoice "
    strsql = strsql & "FROM  openclose!nrcs INNER JOIN "
    strsql = strsql & "openclose!invoice ON Nrcs.file_num = Invoice.file_num "
    strsql = strsql & "Group By Nrcs.file_num"
    This statement gives me the max date OK but the invoice number and amount don't match up with the date.

    I would appreciate any help with this. Thanks.

  2. #2
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    dcarlson

    Off the top of my head I think you will need to add

    WHERE invoice.date = MAX(invoice.date)
    (or somethign to that effect)

    I think you are pulling back the first record as far as the other fields go and the max invoice date in the other field.

    Hope this helps
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  3. #3
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236

    Cool

    Not sure if this will help, but in your SELECT statement, take out the MAX(invoice.date) and just SELECT invoice.date (which will give you that field instead of that date and put a WHERE clause after the innerjoin like this

    strsql = "SELECT Nrcs.file_num, invoice.date, "
    strsql = strsql & "Invoice.amount, Invoice.invoice "
    strsql = strsql & "FROM openclose!nrcs INNER JOIN "
    strsql = strsql & "openclose!invoice ON Nrcs.file_num = Invoice.file_num "
    strsql = strsql & "Group By Nrcs.file_num "
    strsql = strsql & "WHERE invoice.date = MAX(invoice.date)"

    that way you'll SELECT the invoice.date field itself and not select the value.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Sudbury, Ontario, Canada
    Posts
    274
    What a piece of !@#$, I'm using a FoxPro database and this is how I finally got it to work.
    Code:
    strsql = "SELECT Nrcs.file_num, Invoice.amount, "
    strsql = strsql & "Invoice.invoice, Invoice.file_num, Invoice.date "
    strsql = strsql & "FROM openclose!nrcs INNER JOIN openclose!invoice ON "
    strsql = strqsl & "Nrcs.file_num = Invoice.file_num "
    strsql = strsql & "WHERE invoice.date = MAX(invoice.date, {01/01/1990}) "
    strsql = strsql & "GROUP BY invoice.file_num"
    The max function was looking for a secong argument to evaluate which one a of the dates was greater. It wouldn't let me put MAX(invoice.date) on it's own.

    You guys put me on the right track. Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width