Question : Retrieving just the first instance of each customer using SQL

Dear Experts,

I use the following code to pull a list of customers who have placed an order for a particular product between two dates.  However, if the customer has ordered this item more than once in the date period multiple records appear for the customer.  How can I change the SQL to just show me one record per customer?

The SQL so far is:

SELECT Customers.Title, Customers.[First Name], Customers.[Last Name], Customers.Address1, Customers.Address2, Customers.Town, Customers.County, Customers.Postcode, Customers.Country, Sales.[Sales Date], Sales.[Sales Date], SalesD.[Item Code]
FROM Customers INNER JOIN (Sales INNER JOIN SalesD ON Sales.SalesID = SalesD.SalesID) ON Customers.ID = Sales.CustomerID
WHERE (((Sales.[Sales Date])>=#10/1/2007# And (Sales.[Sales Date])<=#10/31/2007#) AND ((SalesD.[Item Code])="BA0002"));

Answer : Retrieving just the first instance of each customer using SQL

I'm assuming you want to return the earliest Sales Date. (See Syntax Below).

While that should do what you want it'll be worth (by that I mean essential) reading up on GROUP BY and the Aggregate functions (MAX, MIN, SUM, COUNT etc.) as they are generally used alot in queries (plus it'll help you understand what your query is doing ).

Hope this helps
1:
2:
3:
4:
SELECT Customers.Title, Customers.[First Name], Customers.[Last Name], Customers.Address1, Customers.Address2, Customers.Town, Customers.County, Customers.Postcode, Customers.Country, SalesD.[Item Code], MIN(Sales.[Sales Date]) as FirstSale
FROM Customers INNER JOIN (Sales INNER JOIN SalesD ON Sales.SalesID = SalesD.SalesID) ON Customers.ID = Sales.CustomerID
WHERE (((Sales.[Sales Date])>=#10/1/2007# And (Sales.[Sales Date])<=#10/31/2007#) AND ((SalesD.[Item Code])="BA0002"))
GROUP BY Customers.Title, Customers.[First Name], Customers.[Last Name], Customers.Address1, Customers.Address2, Customers.Town, Customers.County, Customers.Postcode, Customers.Country, SalesD.[Item Code];
Random Solutions  
 
programming4us programming4us