Last order made by customer from a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alok K Gupta
    New Member
    • Jun 2015
    • 1

    Last order made by customer from a table

    I have 500 customers and 50 type of products with 20 colours each and 5 to 6 type of designs for each product. I want to retrieve data for each customer last order details. Fields in table are
    C_Name P_Name Colour Designs Order date
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    The following query will get the last order by customer. If the field Order date is just a date field and the customer makes two orders on the same day, both orders will be included.
    Code:
    SELECT Lst.C_Name
    	, Table_Name.P_Name
    	, Table_Name.Colour
    	, Table_Name.Designs
    	, Lst.[Order date]
    FROM
    	(SELECT C_Name, Max([Order date]) As LastOrder
    	FROM Table_Name
    	GROUP BY C_Name) As Lst INNER JOIN Table_Name
    	ON Lst.C_Name = Table_Name.C_Name 
    		And Lst.LastOrder = Table_Name.[Order date]

    Comment

    Working...