cs 103: lecture 7. more query features calculated fields – a field that displays the result of and...
TRANSCRIPT
CS 103: Lecture 7
More Query Features
• Calculated fields– A field that displays the result of and expression
consisting of fields, numbers and arithmetic operators
– For example• Wages*(Hours-40)*1.5
– Usually used as part of the result of a query
More Query Features
• Aggregate functions– Functions that perform arithmetical or statistical
operations on selected records– The selected records are either all the records in a
query result or groups (subsets) of the records in a query result
– Common aggregate functions• Average, Count, Maximum, Minimum, Sum
More Query Features
• GroupBy– Divides selected records into distinct groups based
on the values in a specified field– Another way of saying this is that GroupBy breaks
the selected records into subsets based on the values in a specified field.
GroupBy Example• Consider the following course table rows
CnumCname Department CHoursPHY102 Introduction to Physics Physics 4PHY101 Physics for Dummies Physics 1CHM101 Introduction to Chemistry Chemistry 4CHM300 Biochemistry Chemistry 4MTH300 Mathematical Logic Mathematics 3CS400 Theory of Computation Computer Science 3SOC200 A Dummies Guide to Max Weber Sociology 3RUS400 Writing Short Novels I Russian 4RUS401 Writing Short Novels II Russian 3ECO101 Introduction to Economic Economic 3ECO102 Money and Banking Economics 2POL101 Introduction to Political Science Political Science 3POL102 Public Administration Political Science 3CS442 Compilers Computer Science 4CS050 Using a Mouse Computer Science 1CS300 Genetics for Computer Scientists Computer Science 3
GroupBy Example
• How many groups are formed by a GroupBy on Department?
• How many groups are formed by a GroupBy on Chours
• How many groups are formed by a GroupBy on Cnum?
GroupBy Department• Group 1 (Physics)
PHY102 Introduction to Physics Physics 4PHY101 Physics for Dummies Physics 1
• Group 2 (Chemistry)CHM101 Introduction to Chemistry Chemistry 4CHM300 Biochemistry Chemistry 4
• Group 3 (Mathematics)MTH300 Mathematical Logic Mathematics 3
• Group 4 (Russian)RUS400 Writing Short Novels I Russian 4RUS401 Writing Short Novels II Russian 3
• Group 5 (Economics)ECO101 Introduction to Economic Economics 3ECO102 Money and Banking Economics 2
• Group 6 (Political Science)POL101 Introduction to Political Science Political Science 3POL102 Public Administration Political Science 3
• Group 7 (Computer Science)CS442 Compilers Computer Science 4CS050 Using a Mouse Computer Science 1CS300 Genetics for Computer Scientists Computer Science 3CS400 Theory of Computation Computer Science 3
GroupBy CHours• Group 1 (3)
MTH300 Mathematical Logic Mathematics 3ECO101 Introduction to Economic Economics 3POL101 Introduction to Political SciencePolitical Science 3POL102 Public Administration Political Science 3CS300 Genetics for Computer Scientists Computer Science 3CS400 Theory of Computation Computer Science 3RUS401 Writing Short Novels II Russian 3
• Group 2 (4)PHY102 Introduction to Physics Physics 4CHM101 Introduction to Chemistry Chemistry 4CHM300 Biochemistry Chemistry 4RUS400 Writing Short Novels I Russian 4CS442 Compilers Computer Science 4
• Group 3 (1)CS050 Using a Mouse Computer Science 1PHY101 Physics for Dummies Physics 1
• Group 4 (2)ECO102 Money and Banking Economics 2
GroupBy Cnum?
Access Demonstration
• I will give an example of creating a relationship between Section and Course (we discussed this relationship in lecture)
• I will create examples of queries that use calculated fields, aggregate functions and GroupBy on the Orders table.
ER Diagram
SectionSNum
SemesterYearTimeRoom
CourseCNum
CNameDepartment
CHours
Schedule
M 1
Example Section (time and room not shown)and Course Tables
Section
SNum Semester Year CNum
0001 Fall 2005 CS120
0002 Fall 2005 CS103
0003 Spring 2006 ACC221
0004 Spring 2006 CS120
0005 Spring 2007 CS103
0006 Fall 2007 CS103
Course
CNum CName Department CHours
CS103 Database Computer Science
1
CS120 Soft. Eng. Computer Science
4
ACC221 Intro. To Acct.
Accounting 3
Example Section (time and room not shown)and Course Tables
Section
SNum Semester Year CNum
0001 Fall 2005 CS120
0002 Fall 2005 CS103
0003 Spring 2006 ACC221
0004 Spring 2006 CS120
0005 Spring 2007 CS103
0006 Fall 2007 CS103
Course
CNum CName Department CHours
CS103 Database Computer Science
1
CS120 Soft. Eng. Computer Science
4
ACC221 Intro. To Acct.
Accounting 3
Orders Table
ordnum product supplier unitPrice quantity
O1 pencil Acme supply 0.10 100
O2 desk Furniture farm 150.00 30
O3 copy paper Acme supply 20.00 300
O4 toner Acme supply 12.00 5
O5 pencil Office barn 0.05 30
O6 chair Furniture farm 100.00 10
Query Problems
• Find the number of orders made to Acme Supply• Find the number of orders for each supplier that has at least one order• Find the total number of items order for each product• Find the total dollars of orders for each supplier• Find the total dollars of orders for each product