473,396 Members | 1,972 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,396 software developers and data experts.

Filtering unique values, with a bit of a twist

Hi everybody, here's an example of what I'm trying to do with an MS Access query.

Num |Name | Occu | Date
111 |Jon Smith | Painter | 01/01/2012
111 |Jon Smith | Painter | 01/02/2012
111 |Jon Smith | Painter | 01/08/2012
111 |Jon Smith | Artist | 01/04/2012
111 |Jon Smith | Artist | 01/05/2012
111 |Jon Smith | Artist | 01/06/2012
222 |Ted Smith | Baker | 01/01/2012
222 |Ted Smith | Baker | 01/02/2012
222 |Ted Smith | Cop | 01/03/2012
222 |Ted Smith | Cop | 01/04/2012


I want to create a query in MS Access that will filter out all of the duplicate occupations and leave me with the most recent dated occupation for each. So my query would return:

111 |Jon Smith | Painter |01/08/2012
111 |Jon Smith | Artist |01/06/2012
222 |Ted Smith | Baker |01/02/2012
222 |Ted Smith | Cop |01/04/2012

I'm having a heck of a time figuring it out. Anybody have any suggestions?
May 14 '12 #1
4 1351
Rabbit
12,516 Expert Mod 8TB
Use an aggregate query that returns the MAX date and groups by the other fields.
May 14 '12 #2
NeoPa
32,556 Expert Mod 16PB
Your grouping appears to be by [Num] and [Occu]. You need an aggregate query that finds the maximum date within the group, then to use that query as a subquery of another query that does an INNER JOIN to the table on those three fields. Something similar to :
Expand|Select|Wrap|Line Numbers
  1. SELECT tYT.*
  2. FROM   [YourTable] AS [tYT]
  3.        INNER JOIN
  4.        (SELECT   [Num]
  5.                , [Occu]
  6.                , Max([Date]) AS [MaxDate]
  7.         FROM     [YourTable]
  8.         GROUP BY [Num]
  9.                , [Occu]) AS [subQ]
  10.   ON   tYT.Num=subQ.Num
  11.  AND   tYT.Occu=subQ.Occu
  12.  AND   tYT.Date=subQ.MaxDate
May 14 '12 #3
Thanks NeoPa and Rabbit. I think I have it working correctly. The first suggestion by Rabbit actually produced the results I was looking for. But it would only work if those were the only three fields in the query. So then I moved on to NeoPa's suggestion and created two separate queries. To save some hassle I added a primary key to the table and used the inner join based on that. Thanks again!
May 15 '12 #4
NeoPa
32,556 Expert Mod 16PB
You're welcome James. My post was simply a clarification of Rabbit's. There was no real difference between the two suggestions.

I'm not sure what you mean by linking on the PK from your explanation. It may be clearer with the SQL posted, but you have a working solution and I guess that's all that matters.
May 16 '12 #5

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

Similar topics

8
by: nescio | last post by:
hello, i have an array and i don't know the content of it, but i want only unique values. in php there is a function to do this, but how must i do this in javascript? i have tried a lot and...
5
by: jdwyer05 | last post by:
Hello, I am trying to populate a combo box with only unique values. Currently I am using an access database and VB6 Enterprise. The program populates the combo box fine however, there are several...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
6
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.