PC Review


Reply
Thread Tools Rate Thread

complex query to pull unique values

 
 
John W. Vinson
Guest
Posts: n/a
 
      7th Jun 2010
On Mon, 7 Jun 2010 21:23:45 -0400, "JR" <(E-Mail Removed)> 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]
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      7th Jun 2010
On Mon, 7 Jun 2010 21:41:24 -0400, "JR" <(E-Mail Removed)> wrote:

>Same query, except the for the date instead of First, I put Last and it
>works.


sheer coincidence and good luck. Use the subquery instead.
--

John W. Vinson [MVP]
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      7th Jun 2010
On Mon, 7 Jun 2010 23:13:18 -0400, "JR" <(E-Mail Removed)> wrote:

>in the query view or sql view?


You can use either, but in the query grid (which is NOT the query, just a tool
to help build SQL) you'll need to put the subquery - as a SQL string in
parentheses - in the Criteria box. You'll need to adapt the fieldnames and
tablenames to match your actual tables, which I cannot see.
--

John W. Vinson [MVP]
 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      8th Jun 2010
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.

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.

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.

I've tried putting my date field as the first column in the query. It gives
the same result.

How can I solve this date sorting issue?



 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      8th Jun 2010
Of course 5 minutes after I post, I found a solution but I'm not sure how it
works.

Same query, except the for the date instead of First, I put Last and it
works. I get the most recent inspection record per Supervisor/Room combo.
Great.

But when I look at my query results, Inspection ID = 2, but its date is not
the same date Inspection ID = 2 is in the table.

Inspection ID is pk for the table, autonumbered.

Query result: InspectionID = 2, Name/Room = Smith 123, Date = January 6,
2010
Table row: InspectionID = 2, Name/Room = Smith 123, Date = June 6, 2009


So if i want to go back to see the details from that particular inspection
(the most recent of Supervisor/Room), the Inspection ID is incorrect.

Not quite gettin' it........



"JR" <(E-Mail Removed)> wrote in message
news:%232BNd$(E-Mail Removed)...
>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.
>
> 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.
>
> 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.
>
> I've tried putting my date field as the first column in the query. It
> gives the same result.
>
> How can I solve this date sorting issue?
>
>
>



 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      8th Jun 2010
in the query view or sql view?



"John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
news:(E-Mail Removed)...
> On Mon, 7 Jun 2010 21:41:24 -0400, "JR" <(E-Mail Removed)> wrote:
>
>>Same query, except the for the date instead of First, I put Last and it
>>works.

>
> sheer coincidence and good luck. Use the subquery instead.
> --
>
> John W. Vinson [MVP]



 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      8th Jun 2010
Absolute brilliance!

Thanks so much, John.


"John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
news:(E-Mail Removed)...
> On Mon, 7 Jun 2010 23:13:18 -0400, "JR" <(E-Mail Removed)> wrote:
>
>>in the query view or sql view?

>
> You can use either, but in the query grid (which is NOT the query, just a
> tool
> to help build SQL) you'll need to put the subquery - as a SQL string in
> parentheses - in the Criteria box. You'll need to adapt the fieldnames and
> tablenames to match your actual tables, which I cannot see.
> --
>
> John W. Vinson [MVP]



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare columns pull unique values and first duplicate value Dee Microsoft Excel Discussion 0 24th Jun 2010 04:38 PM
Unique Query in not working correctly ... too complex? BobC Microsoft Access Queries 2 27th Dec 2009 04:00 AM
Query to pull out unique records =?Utf-8?B?c3Vlc2hl?= Microsoft Access Queries 4 31st May 2007 07:03 PM
Pull Unique Values From a List/Table Karl Burrows Microsoft Excel Misc 3 2nd Apr 2004 10:27 AM
Help with query to pull unique subset from table earnheam Microsoft Access Queries 0 10th Dec 2003 01:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:17 PM.