Microsoft
Software
Hardware
Network
Question : Mysterious Access Query
Overview
I've run into an issue with an Access query that I'd like to run by other Experts. I already have a work-around, so I'm not interested in that. What I'm looking for is an explanation or some insight to better understand why the query doesn't work as expected.
The Query
YOURTABLE is the only table is involved. It has an ID column which is the primary key and EmployeeID, whiich is also required. It also has many optional columns, but to simplify this question I've eliminated all but three.
An Employee may have multiple records. Each record may have zero-to-all of the optional fields populated. Those not populated are either null or contain an empty string.
I want the query to return 1 row per employee. When an employee has multiple records, I want the one with the most optional fields populated. When there is a tie, I want the one with the highest primary key.
My Approach
I use nested queries to produce the results. YOURTABLECOUNT returns every row with an extra column that counts the optional fields. YOURMAXCOUNT returns just the rows with the most optional fields populated for each employee. Both of those queries work fine.
However, YOURMAXCOUNT produces multiple rows for the same employee. That's expected when there are ties on the highest populated field count. QUERY1 is an attempt to eliminate the ties by selecting the highest primary key from among the ties.
The Problem
I expect QUERY1 to return 1 row for each employee using a correlated sub-query. But it only returns 1 row altogether as if the correlation part is being ignored. Instead of returning the highest primary key from AMONG THE TIES for the same employee, it returns only the highest primary key ACROSS THE ENTIRE TABLE.
I got the expected results in QUERY2, simply by using different table aliases than QUERY1. Seems like that shouldn't matter, but it seems to.
I also got the expected results in QUERY3 with the original table aliases but using a join instead of a correlated subquery.
The Test Database
I realize this is a long-winded question; it is best understood by looking at the attached test database. BTW, I have the problem in both .mdb and .accdb file using Access 2007.
Questions
1. Why does QUERY1 return 1 row instead of 1 row per employee?
2. Why does changing table aliases make a difference?
Attachments:
DBTest.mdb
(244 KB)
(File Type Details)
Mysterious Query Test
Answer : Mysterious Access Query
Android 2.0 upwards supports mutiple Exchange accounts:-
http://developer
.android.c
om/sdk/and
roid-2.0-h
ighlights.
html
Random Solutions
CSS Template
ASP.NET ChartControl
Easy to use student management software
need drivers for these printers, for server 2008 R2 x64
Get DNS to clients when using IPV6 Stateless configuration
Incorrect function. error
EIGRP Feasibility Condition
Upgrading from Windows 7 Home Ultimate to a professional version - how?
Could Linkserver provide me DB schema?
blackberry: PIM Synchronization is not enabled on this device