gil_wilkes said:
I have a table called Data, with fields Home, Away, Referee. What I
want to do is find out which teams (Home or Away) a referee hasn't
had, by entering a referee's name.
To start off, I'd be surprised if "Data" isn't a reserved keyword.
You should consider renaming the table to something a little more
descriptive, like GameData, which I can guarantee is not a reserved
keyword.
This is not a trivial problem. You will need to use some advanced sql
techniques to get your answer. In fact, if you are not familiar with
using the SQL View of the Query Builder, now is the time to start.
With your database open in Access, click into the Queries tab and
create a new query in Design view. Cancel the Choose Tables dialog
without selecting a table. Use the View menu, or the toolbar button,
or the right-click context menu to switch to SQL View. This is where
we will be working.
Now, it's simple to determine which Home teams the referee has not
refereed. Paste this into the sql window:
SELECT DISTINCT [Home] FROM [Data]
WHERE [Referee] <> [Enter Referee Name]
Click the Execute button in the toolbar to run it. Swithc back to
Design View to see what it looks like in the grid.
It is just as easy to figure out which Away teams have not been
refereed. Just change [Home] to [Away].
So, you see the problem: the teams exist in two columns. The
solution is to get them into a single column. How to do that? Well,
here is where the first advanced sql technique I was talking about
comes into play. It's called a "union" query. A union query is used
to combine the data from two separate queries into a single
resultset. What we want to do is select the distinct home teams and
put them into the same column as the set of distinct away teams.
Here is how it works. Paste the following into the sql window:
SELECT DISTINCT [Home] As Team,[Referee] FROM [Data]
UNION
SELECT DISTINCT [Away],[Referee] FROM [Data]
Note that we are now in a sql-only world: Access will not allow you
to switch this query to Design View.
Note also: there is an optional
Run the query to see that we have now accomplished our first
objective: we have a list of teams that each referee has done. And
that list is in a single column.
Note also: there is an optional keyword that will affect the results
of a union query: "ALL". I did not use that keyword, so the query
engine eliminated duplicate records from the resultset. Put the
keyword in to see the difference:
SELECT DISTINCT [Home] As Team,[Referee] FROM [Data]
UNION ALL
SELECT DISTINCT [Away],[Referee] FROM [Data]
See how you now have multiple records with the same team and
referee? Not the intended result so let's take that keyword out of
there. Note that this particular situation is one where the keyword
should not be used. In other situations, for example, when you are
sure that no duplicate results will be obtained, you should use the
"ALL" keyword, because performance will be improved without the
query engine having to perform the extra task of removing duplicates.
Now, we need to use the next advanced sql technique: the subquery.
Ideally, that list of referees and teams would be in a table which
could easily be queried using the first piece of sql I provided
above. The hurdle is to realize that it IS a set of data that can be
queried. A sql statement's FROM clause is not limited to the names
of tables or saved queries: it can also contain an expression (a
subquery) that results in a set of data. Here is how it works:
SELECT DIstinct [Team] FROM
(
SELECT DISTINCT [Home] As Team,[Referee] FROM [Data]
UNION
SELECT DISTINCT [Away],[Referee] FROM [Data]
) As q
WHERE [Referee] <> [Enter Referee Name]
And that's the solution.
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET - 2002-2007
Please reply to the newsgroup. This email account is my spam trap so
I don't check it very often. If you must reply off-line, then remove
the "NO SPAM"