Conjunction and Disjunction in Data Analysis |
|
Logical Disjunction
Introduction
Consider a database that contains a list of students with various pieces of information. Imagine that our main goal is to make sure that we have a number we can use to contact somebody for the student. In this case, either the home phone number or the emergency number would be fine. We already have the following two statements:
To get either number, we can concatenate these two statements and evaluate the truthfulness of either one of them. We create a new statement as follows:
The logical disjunction shows us that a combined statement
is true if EITHER of its two statements IS TRUE. The logical disjunction
produces a false result if BOTH of its two statements are FALSE.
To perform a logical disjunction, you can use the OR operator.
Practical Learning: Introducing Logical Disjunction
Using a Logical Conjunction
To perform filtering by logical disjunction, you can use filter by value, Filter By Form, or use a grid. Consider the following table of videos from a database named Video Collection:
To perform filtering by selecting this or that value:
A window would come up with the values from the column you selected and each value would have a check box. To apply a logical disjunction, put the check mark on at least two values excluding the (Select All) option. Here is an example:
After making the selections, to apply the filter click OK. This would result in the records that use either of the selected values:
To remove the filter, in Lesson 17, we saw that you could click the Toggle Filters button on the Ribbon or the Filtered button on the table, query, or form. As an alternative, you can display the window again, click the (Select All) option and click OK.
To filter by form, open the table, query, or form. in the Sort & Filter section of the Ribbon, click Adv anced -> Filter By Form. Click the combo box under the first desired field and select the desired value. Then click the combo box under the other desired field and select the desired value:
In the bottom section of the window, click the Or tab. Then, on the window, click the combo box under the same column and select another value. Here is an example:
After selecting, apply the filter. The resulting list would include the records that use either of the selected values:
As an alternative, you can select a value from one column for the first value, click the OR tab, then select a value from another column.
To filter using a grid, open the table or form. in the Sort & Filter section of the Ribbon, click Advanced -> Advanced Filter/Sort. Select the field and put it in the bottom section of the window. In the Criteria box that corresponds to the column, type the desired value or expression. In the OR box that corresponds to the same column, enter another value. Here is an example:
After specifying the values, apply the filter. The resulting list would include the records that use one or the other value.
In the same way, to use a logical disjunction on a query, open it in Design View and select all the fields that will be part of the query. In the Criteria box that corresponds to the desired columns type the desired expression and press the down arrow key. In the OR box of the same column, or the OR box of another column, type the desired value or expression. Then display the query in Datasheet View.
All of the types of values or logical operators we reviewed in the previous lesson can be applied to a logical disjunction:
Remember that everything is case-insensitive.
As stated already, the SQL supports the logical disjunction with an operator called OR. Besides the SELECT statement, the formula to use is:
WHERE Expression1 OR Expression2
The WHERE and the OR keywords are required. Each expression is in the form:
FieldName Operator Value
This means that you must specify a column, the operator that performs the filtering, and the value applied to the column. Here is an example:
SELECT Title, Director, CopyrightYear, Rating, Length, Format, [Wide Screen] FROM Videos WHERE (Rating = "Unrated") OR (Rating = "R");
Practical Learning: Using Logical Disjunction
SELECT Properties1.[Property #], Properties1.[Property Type], Properties1.City, Properties1.State, Properties1.Bedrooms, Properties1.Bathrooms, Properties1.Condition, Properties1.[Market Value] FROM Properties1 WHERE (((Properties1.State)="VA")) OR (((Properties1.State)="MD"));
Logical Conjunction
Introduction
Imagine you are reviewing the students’ records of a school and you want to make sure that you know whom to contact in case of emergency for each student. For example, imagine that you are trying to make sure the record of each student provides an emergency telephone number. The statement to evaluate would be:
If the record of the student referred to contains an emergency phone number, the statement is true. If the record does not provide this information, our goal is not met and the statement is false. Imagine that, on the other hand, we want to find out if the record of each student displays the name to contact in case of emergency. The statement considered would be:
If the record considered displays an emergency name, the statement is true.
Imagine that a student's record indicates an emergency telephone number but does not provide an emergency name. What if a student's record indicates an emergency name but no emergency telephone number? It appears that in the absence of one of these pieces of information, it would be hard to perform an effective contact. To make a record complete or to accomplish our goal, we want each record to have both an emergency name and an emergency telephone number. We can combine both of the above statements as follows:
Evaluating the combined statement would produce the following results:
To resume, our goal is met only if BOTH the first and the second statement are TRUE.
On a table or query that displays in Datasheet View, or a form, to combine two statements, you can use Filter by Form and select a criterion for each field that is being considered. Another alternative is to concatenate (join) statements. This is done using the AND operator.
Practical Learning: Introducing Logical Conjunction
Using a Logical Conjunction
To filter by value, open the table, query, or form:
A window would display the values from the column you selected and each value would have a check box. Put a check mark on one value excluding the (Select All) option and leave the others uncheck. Here is an example:
After checking the value of the first field, click OK. Access another field and select one of its values. Here is an example:
After clicking OK, the list would include only the records that use both values:
It is important to know that the order of selecting the fields is not important. In this example, we could have started with the Director field and we would get the same result.
To filter by form, open the table, query, or form. in the Sort & Filter section of the Ribbon, click Advanced -> Filter By Form. Click the combo box under the first desired field and select the desired value. Then click the combo box under the other desired field and select the desired value:
Once again, the order in which you select the values is not important. What is important for a logical conjunction is that you select one value for each of two fields. After selecting, apply the filter. The resulting list would include only the records that use both values:
To filter using a grid, open the table or form. in the Sort & Filter section of the Ribbon, click Adv anced -> Advanced Filter/Sort. Select the first field and put it in the bottom section of the window. In the Criteria box of the column, type the desired value or expression. Add another column to the bottom section of the window. In its Criteria box, type the desired value:
After specifying the values, apply the filter. The resulting list would include only the records that use both values:
In the same way, to use a logical conjunction on a query, open it in Design View and select all the fields that will be part of the query. In the Criteria box corresponding to one of the columns, type the desired expression. In the Criteria box of another column, type the desired value or expression. Then display the query in Datasheet View.
All of the types of values or logical operators we reviewed in the previous lesson can be applied to a logical conjunction.
The SQL supports the logical conjunction using the AND operator. Besides the SELECT statement, the formula to use is:
WHERE Expression1 AND Expression2
The WHERE and the AND keywords are required. Each expression is in the form:
FieldName Operator Value
You must specify a column, an operator, and a value.
Practical Learning: Using Logical Conjunction
SELECT Properties1.[Property #], Properties1.[Property Type], Properties1.City, Properties1.Bedrooms, Properties1.Bathrooms, Properties1.[Year Built], Properties1.Condition, Properties1.[Market Value] FROM Properties1 WHERE (((Properties1.City)="Silver Spring") AND ((Properties1.[Market Value])<=500000));
Combination of Logical Operators
You can apply more restrictive filters by combining logical operators, as long as you follow some rules. Imagine that in your database of students, you want to get a list of girls whose records indicate either a home telephone number or an emergency telephone number. To find these records, you should proceed by logic thinking and combine filters that can produce the right result. Because databases are varied and scenarios are different, there is no strict recipe to follow, just some rules and suggestions.
Imagine that, in your database of students, you want to see a list of girls who live in a Single Parent home. Using the Filter by Form on a table, in the Gender column, you can just select F and, in the Single Parent? column, you would select the check box.
Imagine now that you want a list of male students whose records display either a home telephone number or an emergency telephone number. The expression to evaluate would look like this: "Home Phone Contains Something For Each M Gender" AND "Emergency Number Contains Something For Each M Gender". To create the criteria using Filter by Form on a form, for the first criterion, you would select M in the Gender combo box and, in the Home Phone field, you can type IS NOT NULL (which means that this field must not be empty for the criterion to apply):
Then click the OR tab and proceed the same for both the Gender combo box and the emergency telephone number field:
After applying the filter, you would notice that the list displays only boys whose records have either of both telephone numbers.
Suppose you want a list of girls whose records have incomplete information on who to contact in case of emergency. To satisfy this goal, we want each girl to have either a home telephone number OR both an emergency name AND an emergency telephone number. We believe that, if the record has a home number, we can call her home and ask for Mr. or Mrs. Last Name. If the record does not have that number, we want to have an emergency number. Because we saw earlier (or as our records of some students indicate) that an emergency number is not necessarily the (direct) parent, we want to make sure that we have a corresponding emergency name. Such an expression can be created as follows:
"<'Gender=F' AND 'Home Phone IS NULL'> OR <'Gender=F' AND 'Emergency Name IS NULL AND Emergency Phone IS NULL'>".
Using the Filter By Form on a form, for the Look For section, we would select the F Gender and, in the Home Phone field, we can type IS NULL. Then, we click the OR tab and, in the Gender combo box we select F; in the Emergency Name, we would type IS NULL and, in the Emergency Phone, we type IS NULL:
Practical Learning: Combining Logical operators
SELECT Properties1.[Property #], Properties1.[Property Type], Properties1.City, Properties1.State, Properties1.[ZIP Code], Properties1.Bedrooms, Properties1.Bathrooms, Properties1.Stories, Properties1.[Year Built], Properties1.Condition, Properties1.[Market Value] FROM Properties1 WHERE (((Properties1.[ZIP Code]) Between 20850 And 20920));
SELECT Properties1.[Property #], Properties1.[Property Type], Properties1.City, Properties1.State, Properties1.[ZIP Code], Properties1.Bedrooms, Properties1.Bathrooms, Properties1.Stories, Properties1.[Year Built], Properties1.Condition, Properties1.[Market Value] FROM Properties1 WHERE (((Properties1.[Property Type])="single family") AND ((Properties1.[ZIP Code]) Between 20850 And 20920)) OR (((Properties1.[Property Type])="townhouse"));
SELECT Properties1.[Property #], Properties1.[Property Type], Properties1.City, Properties1.State, Properties1.[ZIP Code], Properties1.Bedrooms, Properties1.Bathrooms, Properties1.Stories, Properties1.[Year Built], Properties1.Condition, Properties1.[Market Value] FROM Properties1 WHERE ((Properties1.[Property Type] = "single family") OR (Properties1.[Property Type] = "townhouse")) AND (Properties1.[ZIP Code] Between 20850 And 20920);
Lesson Summary
MCAS: Using Microsoft Office Access 2007 Topics
S22 | Switch between object views |
Exercises
Yugo National Bank
Watts A Loan
World Statistics
US Senate
|
||
Previous | Copyright © 2008-2019, FunctionX | Next |
|