Access Patterns Karl Lieberherr 11/8/2018 Access Patterns.

Slides:



Advertisements
Similar presentations
AESuniversity Ad hoc Reporting. Ad hoc Reports What are ad hoc reports? Why would you use ad hoc reports? Creating an ad hoc report from a query Building.
Advertisements

 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Access Patterns we have seen. We review some of the common patterns we have used. IMPORTANT NOTE: SQL is given for informational purpose only. We have.
Concepts of Database Management Seventh Edition
Chapter 11 Group Functions
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Access Query Builder: Building Queries with the Principle of Least Information Karl Lieberherr.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
IST 210: ORGANIZATION OF DATA Chapter 1. Getting Started IST210 1.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Database Systems Microsoft Access Practical #3 Queries Nos 215.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
Part II. Query Types On the design query table pane, right click and the cascading window will appear.
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 16 Using Relational Databases.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Week 3 Lab1 Review on Database Dina A. Said
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Access Test Solutions Fall 2014 Questions 7 and 8 Karl Lieberherr.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Chapter 3 Introduction to SQL(2)
Slides are reused by the approval of Jeffrey Ullman’s
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
CS 480: Database Systems Lecture 12 February 11, 2013.
Decision Analysis With Spreadsheet Software
Database Normalization
Chapter 3 Introduction to SQL(3)
Using Relational Databases and SQL
Outline of the ER Model By S.Saha
Database Management  .
Microsoft Access 2003 Illustrated Complete
Chapter 5 STUDENT-COURSE
Chapter 2: Intro to Relational Model
CS 405G: Introduction to Database Systems
Access Final Fall 2014 Query 9
Major League Baseball American League.
Access Quiz.
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
Microsoft Official Academic Course, Access 2016
Chapter 2: Intro to Relational Model
Advanced Database Concepts: Reports & Views
Access: Queries III Participation Project
Chapter 2: Intro to Relational Model
Section 4 - Sorting/Functions
Chapter 2: Intro to Relational Model
Creating complex queries using nesting
Building Queries using the Principle of Simplest Query (POSQ)
Joins and other advanced Queries
Topic 12 Lesson 2 – Retrieving Data with Queries
Major League Baseball American League.
Database 2.
Advanced Tables Access Lesson 9.
Group Operations Part IV.
Presentation transcript:

Access Patterns Karl Lieberherr 11/8/2018 Access Patterns

Patterns It is useful to think in terms of problem solving patterns when writing queries. We follow the Principle of Least Information which divides queries into four pattern kinds: calculated field elimination of duplicates (distinct rows for selected fields) aggregation-Totals (non-trivial aggregation with calculated field) selection We cover two patterns of the aggregation-Totals: AggregateForOther and CountForSelf. Also two composite patterns: ArgMax and AboveAverage Patterns. Disambiguation Pattern. 11/8/2018 Access Patterns

Pattern: AggregateForOther Instantiations and Examples SumForOther Examples: OrderTotals: For each OrderID, sum all extended prices. CourseLoad: For each Student, sum the credit hours. MenuItemUnitsSold: For each menu item, sum the units sold. AverageForOther Example: AverageGPA: For each StudentID, compute the average grade across all courses taken. CountForOther CountCoursesStudent: For each StudentID, count the number of courses taken. CountCoursesInstructor: For each InstructorID, count the number of courses taught. 11/8/2018 Access Patterns

Pattern: CountForSelf Examples CountWins: Count the wins a team made. CountLosses: Count the number of losses a team had. 11/8/2018 Access Patterns

We review some of the common patterns we have used. IMPORTANT NOTE: SQL is given for informational purposes only to summarize the effects of clicking in the Query Builder. We have not covered SQL but used the Query Builder instead. 11/8/2018 Access Patterns

Table ID A B C 1 a1 b1 5 2 a1 b1 8 3 a1 b2 7 4 a1 b2 2 5 a1 b2 1 11/8/2018 Access Patterns

Pattern SumForOther: What we want: summing for distinguished column of another column A SumOfC a1 23 a2 15 a3 0 11/8/2018 Access Patterns

Pattern SumForOther: SQL query SELECT Table1.A, Sum(Table1.C) AS SumOfC FROM Table1 GROUP BY Table1.A; 11/8/2018 Access Patterns

Pattern SumForOther: Query Builder Manipulation Create Query Choose Table1 Select column A Select column C Totals (GroupBy default) Sum for column C 11/8/2018 Access Patterns

Table ID A B C 1 a1 b1 5 2 a1 b1 8 3 a1 b2 7 4 a1 b2 2 5 a1 b2 1 11/8/2018 Access Patterns

Pattern CountForSelf: What we want: Counting for distinguished column B CountOfB b1 4 b2 4 11/8/2018 Access Patterns

Pattern CountForSelf: SQL SELECT Table1.B, Count(Table1.B) AS CountOfB FROM Table1 GROUP BY Table1.B; 11/8/2018 Access Patterns

Pattern CountForSelf: Query Builder Manipulation Create Query Select Table1 Select column B Totals (GroupBy) For second column B choose count 11/8/2018 Access Patterns

Table2 (GameResults) ID winner loser forced 1 1 2 1 1 1 2 1 2 2 1 2 3 1 3 3 4 3 2 2 5 4 1 4 6 1 4 4 7 1 5 0 1 Baltimore Orioles 2 New York Yankees 3 Toronto Blue Jays 4 Tampa Bay Rays 5 Boston Red Sox Forced: Team was handicapped (played without their strongest player) 11/8/2018 Access Patterns

Pattern CountForSelf: What we want: count wins for each team winner CountOfwinner 1 4 2 1 3 1 4 1 11/8/2018 Access Patterns

Pattern CountForSelf: SQL SELECT Table2.winner, Count(Table2.winner) AS CountOfwinner FROM Table2 GROUP BY Table2.winner; 11/8/2018 Access Patterns

CountForSelf: Query Builder Manipulation Create Query Choose Table2 Select winner column twice Totals (GroupBy) For second winner column: Count 11/8/2018 Access Patterns

ArgMax and AboveAverage Pattern Composite pattern involving several other patterns. 11/8/2018 Access Patterns

ArgMax Pattern Example Queries Which are the most expensive menu items? Which are the most expensive orders? Which course has been taken by the most number of students? List the students who have taken the smallest number of credits. 11/8/2018 Access Patterns

Implementation of ArgMax Pattern Use an AggegrationForOther query to create the list of numbers lon of which the maximum is computed (subquery 1). Compute the maximum M (subquery 2). Select the numbers in lon which are =M (use subquery 1 and 2). Use selection query or relationship with join. 11/8/2018 Access Patterns

AboveAveragePattern Examples Which orders are above average? 11/8/2018 Access Patterns

Implementation of AboveAverage Pattern Use an AggegrationForOther query to create the list of numbers lon of which the average is computed (subquery 1). Compute the average A (subquery 2). Select the numbers in lon which are >A (use subquery 1 and 2). Use selection query. 11/8/2018 Access Patterns

Disambiguation Pattern Example Query: How many courses is each instructor available to teach? List the last name of the instructor who can teach and the number of courses. Note: We list the last name and not the primary key (InstructorID). Therefore there is a need for the disambiguation pattern because there may be multiple instructors with the same last name. Name not unique. Use ID to disambiguate but don’t show it. Let’s call this the Disambiguation Pattern. 11/8/2018 Access Patterns

Disambiguation Pattern 11/8/2018 Access Patterns

Required Elimination of Duplicates Pattern Example Query: How many courses use textbooks published by “Wiley”? Note: when we list CourseIDs and TextbookIDs there may be courses that use two textbooks by Wiley. We cannot count the CourseIDs directly and need first an isolated Elimination of Duplicates. 11/8/2018 Access Patterns

First Subquery 11/8/2018 Access Patterns

output 11/8/2018 Access Patterns

Summary We covered two single table aggregation patterns: AggregateForOther and CountForSelf. two composite patterns: ArgMax and AboveAverage. There are many more patterns we covered in class. 11/8/2018 Access Patterns

Table2 (DebateResults) ID winner loser forced 1 1 2 1 2 2 1 2 3 1 3 3 4 3 2 2 5 4 1 4 6 1 4 4 7 1 5 0 11/8/2018 Access Patterns

What we want: count faults Faulter CountOfFaulter 1 2 2 1 5 1 11/8/2018 Access Patterns

Add calculated field for Faulters: subquery What do we want for the subquery? 11/8/2018 Access Patterns

Create Faulter column from Loser column Faulter forced 2 1 1 2 1 4 5 0 11/8/2018 Access Patterns

SQL SELECT Table2.loser AS Faulter, Table2.forced FROM Table2 WHERE (((Table2.loser)<>[forced])); 11/8/2018 Access Patterns

Query Builder Manipulation Create Query select loser column; rename to Faulter; condition <>[forced] select forced column (for checking result) name subquery: Faults 11/8/2018 Access Patterns

Reminder: What we want Faulter CountOfFaulter 1 2 2 1 5 1 11/8/2018 1 2 2 1 5 1 11/8/2018 Access Patterns

SQL SELECT Faults.Faulter, Count(Faults.Faulter) AS CountOfFaulter FROM Faults GROUP BY Faults.Faulter; 11/8/2018 Access Patterns

Query Builder Manipulation Create Query Choose subquery Faults Select Faulter column twice Totals (GroupBy) select Count for second 11/8/2018 Access Patterns