Results 1 to 7 of 7

Thread: Help With SQL Query

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Help With SQL Query

    I have a inventory Table with a list of item numbers.

    I also have a sales table showing all sales. The following field are in the sales table

    Quantity
    DateSold
    ItemNum
    CustomerName


    I need a to show all the items of the inventory table together with the last record of the sales table. So for each item a only need the last sale.

    How can i create such a query?

    Thanks for your help.

  2. #2
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Help With SQL Query

    something like this?
    SELECT Inventory.Description, Max(Sales.DateSOld) AS MaxVanDateSOld, Max(Sales.ItemNUm) AS MaxVanItemNUm, Max(Sales.Customername) AS MaxVanCustomername
    FROM Inventory INNER JOIN Sales ON Inventory.ID = Sales.ItemNUm
    GROUP BY Inventory.Description;

    I've created this query on a MS Access DB. Because you didn't mention wich type you were using. I think it should do the trick.

    Next time ask databasequestions here
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Help With SQL Query

    This will do it:

    Code:
    Select * From SomeTable S1
    Where S1.DateSold=(Select Max(S2.DateSold) From SomeTAble S2
                             Where S2.ItemNum=S1.ItemNum)
    Basically only selecting the rows where the DateSold is the max DateSold for that item.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Help With SQL Query

    Thanks all

    robbedaya:

    the problem is it returns the biggest customer name Max(customername) even if it isn't the same as the max(datesold)

    szlamany:

    What happens when two people ordered the same day? I only want one of them returned.

    I am using MS Access.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Help With SQL Query

    Quote Originally Posted by shragel
    szlamany:

    What happens when two people ordered the same day? I only want one of them returned.
    Well - tell me which one you want returned if two customers order.

    Does anything indicate the "latest" one in the day? Do you simply want the "latest" alphabetical one?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Help With SQL Query

    The "latest" alphabetical one?

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Help With SQL Query

    Well - that does make it a bit more complex.

    Maybe it's best to create two views so that it's easier to see how this is going to work. You could always put the two views into "derived" tables like my first example - but basically it's going to work the same either way.

    These two views give you the hook to the row wanted.

    Code:
    Create View MaxDateSold
       As Select ItemNum,Max(DateSold) "MaxDateSold"
             From SomeTable
    
    Create View LastDateCust
       As Select MD.ItemNum,MD.MaxDateSold,Max(S1.CustomerName)
             From MaxDateSold MD
             Left Join SomeTable S1 on S1.ItemNum=MD.ItemNum
                                    and S1.DateSold=MD.MaxDateSold
    The second view - LastDateCust - should be what you JOIN to first.

    Code:
    Select * From ...
      Left Join LastDateCust LD on LD.ItemNum=... and LD.MaxDateSold=... and LD.CustomerName=...
      Left Join ...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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