From these two results, notice that there is no relationship between the fact that a property has 2 bedrooms and its being vacant. To rent a property for our customer, it must have two bedrooms. We can create a Boolean truth table as follows:
The property to rent must have two bedrooms. This excludes the 1, 3, and more-bedroom properties. This means that if the property has a number of bedrooms other than 2, whether it is available or not, it cannot be rented to the current customer:
Once we have a list of properties that have two bedrooms, now, let's consider the available properties. If a property has two bedrooms and it is available, then it can be rented to the current customer:
If the property has 1, 3 or more bedrooms but not 2, whether it is available or not, it cannot be rented to the current customer:
In the same way, if both conditions are false (the property has 1 or more than 2 bedrooms and in fact it is not available), the result is false (the property cannot be rented to the current customer):
This demonstrates that a property can be rented to the current customer only if BOTH conditions are met: The property has two bedrooms AND it is available. This type of condition is referred to as logical conjunction.
As mentioned in previous sections, before performing data analysis, first display the table or query in Datasheet View, or the form in Form View. After displaying the object, on the Ribbon, click Home. In the Sort & Filter section, click Advanced -> Filter By Form. To perform logical conjunction, select the values of two (or more) columns in the same row. After setting the criteria, you can click the Filter button on the Ribbon to see the result. After viewing the result, to restore the table, query, or form, on the Ribbon, you can click Toggle Filter. When you perform filtering on a form, it fires the On Filter event. When you apply the filter on a form, it fires an On Apply Filter event.
To express the logical conjunction, the SQL uses the AND operator. To use it visually when creating a query, after selecting the columns, in the lower section of the window, click the Criteria box corresponding to each column that will be involved in the conjunction. For example, if you want to create a list of movies released in 1994 but rated R, type the appropriate value in the Criteria boxes of the columns. Here is an example:
To manually create a logical conjunction in SQL, type one condition on the left and the other condition on the right sides of the AND keyword using the following formula: SELECT WhatColumn(s) FROM WhatObject WHERE Condition1 AND Condition2 The WhatColumn(s) and the WhatObject factors are the same we have used so far. The AND keyword is the new one. Each condition is written as a SQL operation using the formula: Column operator Value In this case, the WHERE operator resembles the If conditional statement. The Condition1 is the first that would be examined. Remember that, from our discussion earlier, if the first condition is false, the whole statement is false and there is no reason to examine the second condition. If the first condition is true, then the second condition would be examined. Based on this, the SQL statement used to get a list of movies released in 1994 but rated PG-13 is: SELECT Title, Director, CopyrightYear, Rating FROM Videos WHERE CopyrightYear = "1994" AND Rating = "PG-13"; The equivalent SQL statement of the above query in SQL as written by Microsoft Access is: SELECT Videos.Title, Videos.Director, Videos.CopyrightYear, Videos.Rating FROM Videos WHERE (Videos.CopyrightYear)="1994") AND ((Videos.Rating)="PG-13");
Suppose a customer who is shopping, but is not ready, for a rental property comes to the office and states that she is considering renting. You show her the properties with apartments, townhouses, and single-family homes. At first glance, the customer says she cannot rent an apartment. The other two options are the townhouse or the single family. To prepare the new list, you must create a query that considers only these two options. Before building the query, you can state the following:
We can start a truth table as follows:
To continue with this table, we can check each property. If the property is a townhouse, it is considered valid for our customer:
It a property is not a townhouse. Then, we consider the next property. If the next property is a single family, it is also valid:
When building this table, we would skip a property only if it is neither a townhouse nor a single family. In Boolean algebra, this means that if both conditions are true, the whole statement is also true. The whole statement is false only if both conditions are false. This can be resumed as follows:
This type of statement is referred to as logical disjunction. The logical disjunction is expressed in Microsoft Access and in SQL with the OR operator.
To perform an OR analysis on a table or query, display it in Datasheet View or display the form in Form View. On the Ribbon, click Home. In the Sort & Filter section, click Advanced -> Filter By Form: While in the Look For tab, click the box under the column that would be used to set the first condition, and select the desired value. After selecting the value of the first condition, click the Or tab in the lower left section of the window. Click the arrow of the combo box under the column that would be used as the second condition. After setting the criteria, to apply the filter, in the Sort & Filter section of the ribbon, click Advanced -> Apply Filter/Sort. Once again, if you perform data filtering on a form, it fires an On Filter event.
When creating a query in Design View, the window provides two convenient sections for the first and the second conditions. To set the first condition, click the Criteria box corresponding to its column and type the operation. To set the second condition, click the Or box corresponding to its column and enter the necessary condition. Here is an example:
If you have a logical range of values and you want to know if a certain value is contained in that range, you can use the BETWEEN operator. The BETWEEN operator is combined with AND to get a list of records between two values. The basic formula of this operator is: Expression BETWEEN Start AND End The Expression placeholder of our formula is usually the name of the column whose values you want to examine. The Start factor is the starting value of the range to consider. The End factor is the highest value to consider in the range. After this condition is executed, it produces the list of values between Start and End. Here is an example: SELECT Videos.Title, Videos.Director, Videos.CopyrightYear, Videos.Rating FROM Videos WHERE (Videos.CopyrightYear) Between 1994 And 2004;
If you have a series of records and want to find a record or a group of records among them, you can use the IN operator. The IN operator is a type of various OR operators. It follows this formula: IN(Expression1, Expression2, Expression_n) Each Expression factor can be one of the values of a column. This is equivalent to Expression1 OR Expression2 OR Expression3, etc. Here is an example that shows the list of movies directed by either Oliver Stone or Ron Howard: SELECT Videos.Title, Videos.Director, Videos.CopyrightYear, Videos.Rating FROM Videos WHERE (Videos.Director) In ("Oliver Stone","Ron Howard");
In the previous sections, every time we created a query, we selected the columns we wanted because we knew what fields would be considered in the result. This also meant that we decided (imposed) what would be displayed to the user. In some cases, you may want to let the user specify one category (or more than one category) of values that would show in the result instead of displaying all of the items. Instead of imposing the value to the user, you can create a query that would prompt the user for a value and it would show only the records that are based on the user's choice. This is the basis of a parameter query. A parameter query is one that requests a value from the user and displays its result based on the user's choice. As its name implies, this query expects a parameter, like the arguments we reviewed for procedures. This means that, when creating such a query, you must prepare to display a request to the user. You visually start a parameter query like any other query, by selecting the necessary columns. In the Criteria box corresponding to the column on which the choice would be based, you can enter a phrase between an opening square bracket and a closing square bracket. When creating a parameterized query, you can use the BETWEEN, LIKE, NOT, or IN operators. For example, to let the user enter part of a name of a director, you could set the Criteria of the Director to: LIKE "*" & [A director name that includes] & "*" When the query runs, if the user enters a name such as Phillip, the list would include the 6th and the 12th videos. Instead of requesting just one value as a parameter, you can request more than one. To do this, you can use the BETWEEN operator that requests an additional AND. For example, to ask the user to specify a range of years whose videos you want to see, you would set the Criteria of a CopyrightYear to BETWEEN [Enter a starting year] AND [Enter an ending year]
In previous lessons and section, we saw that the Visual Basic language was equipped with an impressive library of functions. These functions can also be used in queries and even included in SQL statements. The SQL interpreter of Microsoft Access can recognize these functions as long as you use them appropriately.
There are two primary ways you can include a function in a query. If you are visually building the query in the Query window, you can write an expression that includes the function. Consider the following list of employees:
Imagine that you want to create a column in a query and that column should hold the full name of each employee. In a column of a table, you could use an expression such as: Employee: [FirstName] & " " & [MiddleName] & " " & [LastName]
The equivalent SQL statement is: SELECT Employees.DateHired, [FirstName] & " " & [MiddleName] & " " & [LastName] AS Employee FROM Employees; This would produce:
Notice that some employees don't have a middle name in their record and the field includes an extra useless empty space. Imagine that you only want to include a middle initial instead of the whole middle name. You can use the Left$ function to retrieve the first character of the middle name and include the call to that function in your query. To do this visually, if you are creating the query in the Query window, simply enter the function call where the column name would be. For our scenario, column name would be set as follows: SELECT Employees.DateHired, [FirstName] & " " & Left([MiddleName],1) & " " & [LastName] AS Employee FROM Employees; This would produce:
Once again notice that some records don't have a middle initial because they don't have a name. For the records that don't display a middle name, we can write a conditional statement, using the IIf() function, to check it and taking the appropriate action accordingly. Here is the result: SELECT Employees.DateHired, IIf(IsNull([MiddleName]), [FirstName] & " " & [LastName],[FirstName] & " " & UCase(Left([MiddleName],1)) & " " & [LastName]) AS Employee FROM Employees; This would produce: In the same way, you can use any of the built-in functions we reviewed in previous lessons.
Besides the various functions built-in the Visual Basic language, the SQL provides an additional collection of functions that you can use to perform various valuable operations in what are referred to as summary queries. A summary query is used to perform a common operation on the values held by a query. If you have re-occurring pieces of information in various columns of a table, you can create a query that shows them in group. To visually create a summary query, start the query in the Query window and select the desired table(s). In the Design tab of the Ribbon, in the Show/Hide section, click the Totals button: To support summary queries, you can create a GROUP BY statement. To do this, at the end of the SQL statement, precede the name of the column with the GROUP BY expression. Here is an example: SELECT Videos.CopyrightYear FROM Videos GROUP BY Videos.CopyrightYear;
To actually perform the necessary operation(s), a query uses a series of functions referred to as aggregate. If you are working visually, after selecting the bas column, use the other columns to set a criterion or the criteria. To programmatically set a condition in a summary query, you precede the condition with the HAVING operator. Here is an example: SELECT Videos.CopyrightYear, Count(Videos.CopyrightYear) AS CountOfCopyrightYear FROM Videos GROUP BY Videos.CopyrightYear HAVING (Videos.CopyrightYear) Is Not Null; To perform its various operations, a summary query relies in what are referred to as aggregate functions: In reality, a summary query uses some of the functions that ship with Microsoft Access:
If none of these functions is suited for the type of statistic you want to get, you can write your own expression or condition. To do this, select the Expression or the Where item. Then, in the Criteria box of the column, type the desired expression. If you select the Where option, type a valid Boolean expression that can evaluate to true or false. |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|