You are browsing the archives of database.
As promised, here is the query within a query solution that simplifies the last article’s idea.
We saw in my last post how a question like “Who was the first person to borrow a book?” could be solved by having a query use the results from another query. That required 2 actual queries saved separately. Now let’s see that same concept done in a single saved query.
Let’s break the problem down into steps.
Step 1 – determine the earliest case of a book being borrowed
SELECT Min(tblBorrowing.dtmDateDue) AS EarliestDate FROM tblBorrowing WHERE (((tblBorrowing.lngBookID)=1));
I have shown the raw SQL above (good idea to learn that stuff!), but you could have just used the query builder to create a query like this following.
Note my use of a custom field name in “EarliestDate”.
Step 2 – Utilize the first query in a where clause
Note how I went back and created the original query style, utilizing all the tables and desired fields, and then added the first query as a criteria for the date field.
This creates a single saved query, rather than 2 separate ones. Here is the resulting (and somewhat hard to read!) SQL code.
SELECT tblCustomers.strCustomerName, tblBooks.strBookName, tblBorrowing.dtmDateDue FROM (tblBooks INNER JOIN tblBorrowing ON tblBooks.lngBookID = tblBorrowing.lngBookID) INNER JOIN tblCustomers ON tblBorrowing.lngCustomerID = tblCustomers.lngCustomerID WHERE (((tblBooks.lngBookID)=1) AND tblBorrowing.dtmDateDue = (SELECT Min(tblBorrowing.dtmDateDue) AS EarliestDate FROM tblBorrowing WHERE (((tblBorrowing.lngBookID)=1)); ));
I will admit, however, that I often clutter my databases with the 2 (or more!!!) query solutions as discussed last week. I find them easier to comprehend and fine tune.
If you want a cleaner looking Queries tab, use this sub-select version!
Some of my Access Database using readers may not know that a query can use another query for its source. Many of us get into the mindset that a query only reads from a table.
So, if I create a new query and choose Design View, the next screen that pops up is called the Show Table dialogue box. But look closer. It is not only showing Tables. There are also tabs for Queries and Both.
When is this useful?
One of the best examples is a library book borrowing database. Answer questions like this. Who was the first person to borrow the book, “How to Create a Query”?
Here are the tables of a simple version of this concept.
tblBooks --------- lngBookID strBookName tblCustomers ------------- lngCustomerID strCustomerName tblBorrowing ------------ lngBorrowID lngBookID lngCustomerID dtmDateDue
The first thought may be to create a “Totals” query that asks for the Min function of the date field. Like this.
This does not work directly, as it returns the first time that each customer borrowed that book. We just want the very first customer who borrowed the book. We can visually see that it was David Martin, but we want the query to determine this by itself.
One solution to this problem follows.
Remove the Customer Name from the first query. Since it is not needed anymore, remove the Customer table as well. The query now returns just one record; that of the first time the book was borrowed. Save that query as qryMinQueryBook, or similar.
Now, start a new query. Add in the query you just created, as well as the Customer table and the Borrowing table. Link the 2 date fields.
This will produce a single record; that of David Martin.
One term that is used for this method is “Cascading Queries”. I have personally used this technique to multiple layers, and horizontally had more than 1 query as well.
In my next article I will show how simple cascading queries like the one above can been done in a single query.
In MS Access, what is a query?
This series of articles on MS Access Queries will start off from raw basics. Most of my regulars will already know this material, and so please allow for the fact that this is meant for “newbies” to some degree. I promise to get into more challenging topics as we go along.
Simply put, a query is a question asked of the database, specifically of one or more of the tables in the database. A query’s answer returns values from selected fields (columns) and records (rows).
A basic example could be, “Which of our customers are located in Springfield?”
A more complex query (or question) could be, “Which of our customers in Springfield have not ordered any products in the past 6 months?”
The above examples are of just one type of query; the select query. MS Access has a number of types of queries available.
This series of articles will be written based on Access 2000. Newer versions of Access may have added to this list. The principles explained here should work fine on version 2000 and any newer ones.
Types of Queries
If you create a query manually (more on this later) you choose from a list of 6 query types.
- Select Query retrieves data from one or more tables (or queries!)
- Crosstab query displays summarized data in a column/row format (like pivot tables in Excel)
- Make Table query creates a new table based on data from one or more existing tables (or queries!)
- Update query updates the data in a table (a simple example would be “Add an across the board raise of 2% to each employee’s pay rate”)
- Append query adds new records (rows) to a table
- Delete query will delete records (rows) from a table
Most of the above queries accept criteria (parameters) such that only some of the records in a table are retrieved or updated.
NEXT: How to create a basic query.
Continuing with my series about using Access on a PC to feed a MySQL database on the web! Read part 3 here. This part describes the structure of the 2 databases involved. One is an MS Access database on a local PC. The other is a MySQL database on a webserver.
Remember that this method was created for websites that exist on servers that do NOT allow remote MySQL connections. Some servers do allow the remote connections, which simplifies the whole process.
While designing a relational database, it is a good idea to distribute the information in multiple tables. It is not advisable to store all the information in a single table, although it is easier to design. When your database grows in size, the efficiency decreases accordingly.
To read the rest of this article, Relational Database Design Guidelines – an introduction.