Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphical Interface for Queries

Similar presentations


Presentation on theme: "Graphical Interface for Queries"— Presentation transcript:

1 Graphical Interface for Queries
Farrokh Alemi, Ph.D. Narrated by Vikas Arya Welcome. This is the lecture on queries as part of the course on healthcare databases.

2 Objectives To develop queries and select subsets of data from a table
Also see “Teaching yourself Visually Access 2003” pages 192 through 226 The objective of this section is to introduce you to making queries on databases. In particular, we want you to be able to select subsets of data through using various methods of filtering the content of a table. This lecture corresponds to pages 192 through 226 in assigned book titled “Teaching yourself Visually Access 2003.”

3 Standard Query Language
SELECT Claims.PatientID, Last(ICD9.ICDDescription) AS LastOfICDDescription FROM Claims INNER JOIN ICD9 ON Claims.DiagnosisCode = ICD9.ICD9Codes WHERE (((ICD9.ICDDescription) Like "*diabete*")) GROUP BY Claims.PatientID ORDER BY Claims.PatientID; The standard query language, or SQL, is the way most database managers use to manipulate data in a database. Here you see an example of this language. In a typical SQL command, the analyst specifies the tables from which the data should be read, the way tables should be related to each other (joined together), how the data should be manipulated or filtered and what should be displayed. Later we will cover SQL commands but for this lecture we prefer to start with graphic interfaces that students can use to generate SQL commands to databases. Such graphic databases allow you to point and click to create a query.

4 A Graphic View This is Microsoft Access’s view for generating a query. You find this query window by opening a database and select the option to design view of the query. You can go back and forth between the graphical and SQL view of the query by clicking on the View button in lower left toolbar.

5 Toolbars The top part is called the tool bar. There are many database options here. The first row is focused on database options, like opening a database. The second row shows the query toolbar, displayed here in graphical icons. On the left lower corner you see a button that allows you to see various views of a query. For example, you can see an SQL view of the query. Next to it you see the icon for saving the changes you have made to query. To the right of save, you see the option to search as well as a series of typical options available in any software such as print, copy, cut, paste, undo and redo. To the right of redo you see the option of setting type of query and next to it the exclamation mark is for running a query. Next to run option you see the button for showing tables on which the query is run. Obviously you must specify which of the many tables in your database are involved in the query. To the right, you see the Total button shown as a summation sign. This sets the option for summarizing several rows of data into one row, for example, when you sum the data in several rows to show as one value. To the right of the total button, you see a command to restrict how much of the output of the query should be displayed. For example, if you want only the first few rows of the query results displayed, you can do so using this option. Still further to the right is the option query properties, for example, here you can specify that values used should be unique and non-duplicating. Still to the right of this you see a wand. This allows you to use internal Access functions to calculate a new field from existing fields in the tables. To the right of the wand you see the option to return to the database view, so you can go back and forth between query window and database window. To the right of this feature you see the new object icon, where you can automatically generate reports from a query that you have created.

6 Place to Show Tables Right below the tool bar, you see an area where the tables involved in the query and their relationships can be displayed. You might use this area to specify several tables and set the keys that these tables share and whether the shared key must be present in all tables or a subset of tables. More on this later.

7 Place to Specify Fields
At the bottom of the window, you can see the space for entering the various fields we would like to display in the query. Some of these fields may come from different existing tables. Others may be calculated. For each field listed we need to specify where it comes from. If it comes from a table, in the second row we specify the table. We can also specify whether the field should be used to sort the data in the query display. In the fourth row we have the option of showing the field or hiding it from the display. The last two rows are for criteria that can be used for filtering the field.

8 Including Relevant Tables in the Query
The first step in constructing a query is to include all relevant tables. If a field is used in a query, a table containing the field must be included in the query. This is done through show table icon, which opens a list of queries and tables that one can include. If more than one table is included, one must also specify the relationship between the tables. The relationship between the tables is specified by draging one field from one table on top of another field in another table. This leads to a join between the tables where the content of the two tables are joined together in a separate table if and only if the two fields match each other. For example, one could use patient’s ID in the claims table to join it to the primary key of the patient’s table. Or one could use the diagnosis code in the claims table to a table describing the code.

9 A query can have as its input another query
Several Queries A query can have as its input another query You can set up several queries, one feeding to another. In this fashion, you can do several operations on the data sequentially. For example, one query can count how many cases you have and the next query can use this information to calculate a percent in each record.

10 Including Appropriate Fields
Fields can be included from all joined tables. In this fashion, a select query makes information in various tables available in one setting. Fields can be dragged from the shown tables to the field cell or one can click on the right of field cell and select a field.

11 Computing a New Field A new field can be calculated from one or more fields in the joined tables or queries. The easiest way for doing so is to put the cursor into the cell for fields, then click on the expression builder icon, the wand. A new window opens that allows you to type the name of the new field followed by colon and followed by the expression to be used to calculate the field. The new name should not contain space or if it does have space it should be put inside brackets. The expression could be composed of built in functions available in Access. Under functions, you will see many examples of existing built in function. The most commonly used function is iif, which allows one to assign a value if a test is met and otherwise assign a different value. There are also numerous functions for dates, the most used one is for calculating difference between two dates.

12 Examples of Calculations of New Fields
Diagnosis: iif(ICD9!Description like “*diabetes*”, “Diabetes”, “Other) If the field Description in the table ICD9 contains the word diabetes assign diagnosis the value “Diabetes” otherwise assign diagnosis the value “Other.” DayTillNow: Date()-Claims!Date Set the field DaysTillNow to be equal to the number of days of difference between today and the field called Date in the table called Claims. The command Diagnosis colon iif open paranthesis ICD9 exclamation mark Description like quote star diabetes star quote comma quote Diabetes quote comma quote Other quote end parenthesis has the following meaning. If the field Description in the table ICD9 contains the word diabetes assign diagnosis the value “Diabetes” otherwise assign diagnosis the value “Other.” DayTillNow: Date()-Claims!Date Set the field DaysTillNow to be equal to the number of days of difference between today and the field date in table Claims.

13 Types of Queries There are six different types of queries allowed in Access. The most widely used query is Select query, where a criteria is used to select and display a subset of one or more tables. The crosstab table is used to table with one field as the rows in the table and another field as the columns in the table. Usually the cell values in a crosstab query is the count of a third field. Besides these two types of queries there are also a number of queries that lead to direct actions on the tables, these are make, update, append and delete queries that do the actions their names implies on a table. The option to change a query type is available on the query toolbar and can be reached by right clicking on the show tables area.

14 Purpose of Select Query
Show only rows that meet criteria In the select query, the rows in a table are reduced to the rows that meet a set of criteria. For example, we might have a table of claims and want to restrict it to patients who had a claim of influenza. Microsoft Access allows several methods for selecting a subset of a table. One way is to restrict it to the cases that match a key in another field. This is typically done when setting the relationship among tables. Another way is to restrict the content by criteria that a field should meet before it is allowed to be included in the display.

15 Selecting through Types of Joins
First, you need to show the tables that are used in the query using the show table icon. Once the tables have been added to the show table area of the query, then you can set the relationship among the table by setting different types of joins. There are three different joins. The typical join is when the fields in both tables have to be exactly the same before the content of the tables are joined together. The other two allows the field in one table to be always included and the field from the other table included only when it matches. When it does not match the record is still kept but there will be a null value in place of the missing match. For example, one might have a table claims that contains diagnosis codes. The meaning of these diagnostic codes might be available in a separate table called diagnosis codes. A join can select the text for the diagnostic code and combine it with the claim data. A one to one join will lead to listing of all claims in which the diagnostic code has a corresponding text in diagnosis table. In a one to one join in which values of diagnosis code should be matched exactly in both tables, If the diagnosis code is missing in the diagnosis code table, then all corresponding claims will not show. In a one to many, we can display all claims and their corresponding diagnosis. Where the diagnosis code is missing in the diagnosis code table, a missing value is entered.

16 Select by Criteria You can specify criteria for a field by enter an expression in the Criteria cell for that field. An expression calculates a single value from a combination of fields using mathematical operations, word operations, or logical tests. In this example, the diagnosis descriptions is restricted by the criteria that it should contain the word diabetes. Like is one of the built in functions available through Access. The like function is followed by quotes that contain star or question mark. A star means any number of letters and question mark means 1 additional letter. A star before and after a word means that any word can follow or preceded the target word. In here a sentence such as diabetes meleitus is matched because it contains the word diabetes and is followed by other words. The sentence Severe Diabetes is also matched because a word precedes diabetes. None of these two sentences are exact match to the word diabetes as they contain other words. If we had asked for an exact match there would have been zero records selected.

17 Using Expressions in Criteria
To set up a criterion, you put your cursor inside the cell and click on the expression builder icon, the wand. You can now use any combination of field names from tables and queries that have been included in the query. There are a number of built in functions available. These include the function iif for testing an expression and various functions for dates.

18 Examples of Criteria Criteria Meaning >120 Greater than 120 Is null
No data in the field > 6/12/05 Passed June 12th 2005 Not diabetes Not matching diabetes Not “*lost” Not any text ending with the word lost Like dia* Any text that starts with dia <Date() Before today’s date Between A and D Text starting with A, B and C

19 Summarizing Several Records
You can summarizes several records into one value by using built in functions such as group by, where all records having the same value will be grouped into one, average where in all records in the group are averaged or standard deviation where in the standard deviation of all records in the same group are calculated. A common function is count where all values in the group are counted and the count is entered. Max and min functions will list the maximum and minimum value for the field in the group. In addition, you can use non-mathematical functions such as last or first to select the last or first value in the field in the group. This feature is added to a query by selecting the icon that looks like a summation sign. When you do so, a row is added to the fields area called “total”. IN this row you have to select which of the many features you want to use. In the example shown, the records are grouped by patient ID by entering under the field PatientID the total function Group BY. The last diagnosis listed in the group is selected. This is shown by the fact that we have entered the function last in the total row under the ICDDescription field. If you need to select a subset of the query, you can use the function Where in the total row. You see this under the field ICDDescription. This field is entered twice in the query because the Where function cannot be displayed in the query. Please note that the Where function is executed before the group function is executed. Thus if a patient has three diagnosis, two of which are diabetes related and one of which is not, the group by will select 3 records without the where function and select 2 records with the where function.

20 Run a Query Once you have organized your query you can select the exclamation icon to run the query. You can also run a query by selecting from the view option at the lower left corner of the toolbar the tabular view of the query.

21 Other Types of Queries Crosstab Action queries
Crosstab query creates a table view of the two fields in the query Action queries make a table, delete selected records, or append selected records to a table.

22 Purpose of Crosstab Query
Organize summary data into tabular format A crosstab query takes the data from different columns of the table and organizes a summary of them into a table. Here the billing data is broken into two groups, less than one thousand and more than one thousand. The diagnosis are broken into diabetes and other diseases, We count the number of patients that fall into every cell.

23 Crosstab Query A crosstab query allows us to classify records based on content of two fields. To start with, each field should be summarized using group by command. You can create a crosstab by deciding which field will be the row heading, which field you want to be the column heading and which field do you want to summarize in the cells inside the crosstab table you are producing. Again keep in mind that you should use a function to summarize the content of records picked up for the cell values. For example, you can select as row headings the gender of the patients and as column headings their age calculated in decades. For the cell content you may select count of records. Thus you will have a count of patients in each cell. Keep in mind that only one field can be used for column heading while several fields can be used for row headings.

24 Change Display of Query Results
You can change the display of query results in several ways. You can sort the display by using the sort row in the lower part of the query design window. You can also select a field by clicking on the bar right above it and drag it to a different position in order to change the order of fields being displayed. Finally you can rename a field by typing before its name a new name followed by colon. You can change the number of decimals displayed in a calculated field by using the Round function in the calculation. But you cannot change how numbers or dates are displayed in the query without change their format in the original table.

25 Graphical view of query makes it simple
You can only learn using queries by doing many of them This ends our brief introduction of Microsoft Access graphical interface for creating a query. A lot is left unsaid. There many functions that you can use. You can only learn the various features by looking at some of the videos we have prepared for you and by doing many queries yourself. Practice makes perfect. The take home lesson in this lecture is that many features are available and that you can do best by starting to use some of them.


Download ppt "Graphical Interface for Queries"

Similar presentations


Ads by Google