473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can I select unique records based on date?

I have a table tracking purchases by customers and want to build a query that pulls each customer's last purchase. To simplify the example, let's say the table is called Customers and has ID, Name, and Purchase_Date. I'd like to get every ID and Name and only the last purchase.

I tried the query below but it only pulls records with the latest date, not the latest date of every record.

SELECT ID, Name, Purchase_Date
FROM Customers
WHERE Purchase_Date = (SELECT max(Purchase_Date) from Customers);

Any thoughts?
Mar 4 '11 #1

✓ answered by beacon

Hi Erik,

Is the ID unique to each Customer? If so, then the following query should provide you with the last purchase date for each customer:
Expand|Select|Wrap|Line Numbers
  1. SELECT Customers.ID, Customers.CustomerName, Max(Customers.PurchaseDate) AS MaxOfPurchaseDate
  2. FROM Customers
  3. GROUP BY Customers.ID, Customers.CustomerName;
  4.  
If the ID is for the order/purchase and you have a separate unique ID for the customers, you'll need a query that just pulls the customer's ID with the MAX of the purchase date, then join this query with another query that includes the max purchase date from the first query and the customer's name from the table where you're storing the customer's name.

Hope this helps and good luck,
beacon

1 3067
beacon
579 512MB
Hi Erik,

Is the ID unique to each Customer? If so, then the following query should provide you with the last purchase date for each customer:
Expand|Select|Wrap|Line Numbers
  1. SELECT Customers.ID, Customers.CustomerName, Max(Customers.PurchaseDate) AS MaxOfPurchaseDate
  2. FROM Customers
  3. GROUP BY Customers.ID, Customers.CustomerName;
  4.  
If the ID is for the order/purchase and you have a separate unique ID for the customers, you'll need a query that just pulls the customer's ID with the MAX of the purchase date, then join this query with another query that includes the max purchase date from the first query and the customer's name from the table where you're storing the customer's name.

Hope this helps and good luck,
beacon
Mar 5 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rado | last post by:
Hi All, This might quite simple process for some but I am finding it really difficult to do. What is required is not a standard Duplicate query but a variation on it. For example I have...
3
by: Beat Scheidiger | last post by:
I do not quite understand this property. Everything is seems clear to me, when I read the corresponding help text. But in practice I have a question: Why are there 3 identical records in the...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
1
by: arthur-e | last post by:
How can you select records based on more than one combo box - I have a combobox that selects records based on name (I'm sure this has been asked a thousand times - web site answer/link could be...
1
by: Kathie G via AccessMonster.com | last post by:
Hello, I have a delima that I am not sure how to resolve. Records get entered manually or through an import. The records contain, RecordID, Client ID, Specimen Date, Visit Date, Client DOB, and...
4
by: Eugene Anthony | last post by:
I have a table that has a DateTime column which uses a DataTime datatype. How do I retrieve a range of records based on the month and year using ms sql? Eugene Anthony *** Sent via...
1
by: thomasburrows1 | last post by:
If one had a massive Table with lots of duplicate rows for most of the information. Speaking of the street address, City, State, ZIP. Given the following table: First name - Char 20 Middle...
3
by: IsValidUN | last post by:
How do you select unique records? For example, if my data is like the following and I only want the repeated data (address information) to appear once for each theater_id. <xsl:for-each...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
8
by: cehlinger | last post by:
I am trying to add multiple records based on a date range. I found some examples and have tried them but am getting errors and I can't seem to figure out what the problem is. I am not a VBA...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.