Selecting where another record doesn't contain a similar reference (1 Viewer)

cj-1289

Registered User.
Local time
Today, 06:55
Joined
Jun 12, 2009
Messages
29
Hello all, I'm trying to think of a purely SQL way of selecting records from a single table for a listbox where the data for the project's reference cannot be like another. To illustrate, the table might have three records containing these values in the reference field like this:

REFERENCE
A-134L
B-134L
A-135L

and the output of the query should be:

REFERENCE
A-135L.

If there is a B-type reference corresponding to the A-type reference, then the original A-type reference cannot be selected. But if there is no such B-type reference, then the A-type reference IS selected. This result would be easy enough to achieve with some VBA behind it but I'm ideally wanting it to be purely SQL. Any thoughts? Thanks in advance.
 

Cerial

Registered User.
Local time
Yesterday, 22:55
Joined
Aug 16, 2012
Messages
12
I created a References table with only ID and Reference as its columns and populated it with your data. I then wrote this query that obtains the output you requested:

SELECT References.Reference
FROM [References]
WHERE (((Mid([Reference],2)) Not In (SELECT Mid([Reference],2) FROM [References] GROUP BY Mid([Reference],2) HAVING COUNT(*) > 1)));
 

Users who are viewing this thread

Top Bottom