A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

complex query to pull unique values



 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old June 7th, 2010, 03:28 AM posted to microsoft.public.access.queries
John W. Vinson
external usenet poster
 
Posts: 18,261
Default complex query to pull unique values

On Mon, 7 Jun 2010 21:23:45 -0400, "JR" wrote:

I have an inspection table to keep track of rooms in various buildings.
These rooms are shared with different supervisors. One inspection of a
location would result in x # of inspection records. If the room was shared
by 3 supervisors, the same inspection would generate 3 inspection records.
Comment field on each record would be different according to the supervisor,
were their employees following protocol, safety issues etc.

Since there are so many fields, trying to pull unique values is difficult,
so I made a new field in the query which concatenates SupervisorID, Bldg &
Room to make a single unique value. This part is fine.


Fine... but unnecessary. You can group by up to TEN fields; it is neither
necessary nor particularly helpful to create a redundant concatenated field.

I can pull unique Supervisor/location records with no problem. However, I
only want the most recent inspection. I set the Totals part of the
concatenated field to "Group By" and the rest to "First". Even though the
dates are sorted as descending in the query, it pulls the First date from
the original table, which by default is sorted ascending. So essentially I
get the most outdated inspection, not the most recent.


First is misleading. It returns the first record *in disk storage order* -
essentially an arbitrary, meaningless record. A Subquery with a criterion
such as

=(SELECT Max([datefield]) FROM tablename AS X WHERE X.SupervisorID =
tablename.SupervisorID)

will be a better approach.

I've tried a two query approach. Make the first query, just sort the dates
descending. Then make a new query based on the first, with the concatenated
fields and so on. It gives the same result.


Because of the same problem - FIRST isn't the "first" in the way you would
think.
--

John W. Vinson [MVP]
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:19 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.