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:
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, 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 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:
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. On the Ribbon, click Home. 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 Properties.[Property #], Properties.[Property Type], Properties.City, Properties.State, Properties.Bedrooms, Properties.Bathrooms, Properties.Condition, Properties.[Market Value] FROM Properties1 WHERE (((Properties.State)="VA")) OR (((Properties.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, the query, or the form. On the Ribbon, click Home. 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. On the Ribbon, click Home. In the Sort & Filter section of the Ribbon, click Advanced -> 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 Properties.[Property #], Properties.[Property Type], Properties.City, Properties.Bedrooms, Properties.Bathrooms, Properties.[Year Built], Properties.Condition, Properties.[Market Value] FROM Properties1 WHERE (((Properties.City)="Silver Spring") AND ((Properties.[Market Value])<=500000));
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 Female:
Then, click the Lives in a Single-Parent Home? check box:
By clicking Toggle Filter, this would produce 15 records:
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 Male gender" AND "Emergency Number contains something for each Male gender". To create the criteria using Filter by Form on a form, for the first criterion, you would select Male in the Gender combo box. Then, in the Home Phone field, type IS NOT NULL (which means that this field must not be empty for the criterion to apply):
Then, click the OR tab. Again, in the Gender combo box, select Male. In the Emrgcy Phone field, type IS NOT NULL:
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 (called the guardian), we want to make sure that we have a corresponding emergency name. Such an expression can be created as follows: "<'Gender=Female' AND 'Home Phone IS NULL'> OR <'Gender=Female' 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 Female 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 Female again. In the Emergency Name, we would type IS NULL and, in the Emergency Phone, we type IS NULL:
Practical Learning: Combining Logical operators |
||
SELECT Properties.[Property #], Properties.[Property Type], Properties.City, Properties.State, Properties.[ZIP Code], Properties.Bedrooms, Properties.Bathrooms, Properties.Stories, Properties.[Year Built], Properties.Condition, Properties.[Market Value] FROM Properties1 WHERE (((Properties.[ZIP Code]) Between 20850 And 20920));
SELECT Properties.[Property #], Properties.[Property Type], Properties.City, Properties.State, Properties.[ZIP Code], Properties.Bedrooms, Properties.Bathrooms, Properties.Stories, Properties.[Year Built], Properties.Condition, Properties.[Market Value] FROM Properties1 WHERE (((Properties.[Property Type])="single family") AND ((Properties.[ZIP Code]) Between 20850 And 20920)) OR (((Properties.[Property Type])="townhouse"));
SELECT Properties.[Property #], Properties.[Property Type], Properties.City, Properties.State, Properties.[ZIP Code], Properties.Bedrooms, Properties.Bathrooms, Properties.Stories, Properties.[Year Built], Properties.Condition, Properties.[Market Value] FROM Properties WHERE ((Properties.[Property Type] = "single family") OR (Properties.[Property Type] = "townhouse")) AND (Properties.[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 © 2010-2019, FunctionX | Next |
|