84136 - Week 7Group by Concept The Group by clause is used to rearrange a table into categories These categories are then analysed usually using a statistical function example Wh
Trang 184136 - Week 7
Week 7 - SQL more complex problems
unit: 84136
by
Trang 384136 - Week 7
Overview
group by concept
(alternative to week 6 work)
multiple group by queries
useful product queries
simple query nesting
relational calculus and SQL
more examples
(reading 3: p.25-p.28)
Trang 484136 - Week 7
Group by Concept
The Group by clause is used to rearrange
a table into categories
These categories are then analysed usually using a statistical function
example What is the total assessment for each student?
Trang 584136 - Week 7
Group by Process
Steps to guide group by queries
Step 1: the partition the table into separate row categories
(group by ) Step 2: the statistical calculation operations performed on one or more columns Step 3: the output
one row per category (select )
Steps to guide group by queries
Step 1: the partition
the table into separate row categories
(group by )
Step 2: the statistical calculation
operations performed on one or more columns
Step 3: the output
one row per category (select )
Trang 684136 - Week 7 ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
example 1 GROUP BY Item the Results table,
P 92 Edmond
Trang 784136 - Week 7 ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
ITEM ID SUBMITTED MARK -
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
method the table is partitioned into separate row categories for each item ITEM ID SUBMITTED MARK -
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
ITEM ID SUBMITTED MARK -
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
example 1 GROUP BY Item
Trang 884136 - Week 7 ITEM ID SUBMITTED MARK
Trang 984136 - Week 7 ITEM ID SUBMITTED MARK
Trang 1084136 - Week 7
ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
locate Min(Mark)
for each group
example 1
GROUP BY Item
Statistical calculation
Circle the answers
Trang 1184136 - Week 7 ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
Step 2:
locate Min(Mark) for
each group
Min(Mark) = 55 when Item = 1 (that is assignment 1)
Trang 1284136 - Week 7 ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
Min(Mark) = 65
when Item = 2
(that is assignment 2)
Step 2:
locate Min(Mark) for
each group
Trang 1384136 - Week 7 ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
Min(Mark) = 40
when Item = 3 (that is assignment 3)
Step 2:
locate Min(Mark) for
each group
Trang 1484136 - Week 7
SELECT Item,Min(Mark)
FROM Results
GROUP BY Item
Step 3:
the output
ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
Result??
Trang 1584136 - Week 7
Result
ITEM ID SUBMITTED MARK
-
1 871 908 80
1 862 907 60
1 854 908 70
1 872 910 55
1 868 906 90
1 869 909 70
2 871 1021 70
2 869 1022 80
2 872 1021 65
2 862 1022 70
2 868 1021 75
3 869 ? 95
3 872 ? 45
3 862 ? 40
3 868 ? 50
3 871 ? 60
Step 3:
the output
SELECT Item,Min(Mark)
FROM Results
GROUP BY Item
Trang 1684136 - Week 7
SELECT Item,Min(Mark)
FROM Results
GROUP BY Item
Step 3:
the output
ITEM MIN(MARK)
-
1 55
2 65
3 40
* END OF RESULT ****
* 3 ROWS DISPLAYED *
Output
Trang 1784136 - Week 7
SELECT Item,Min(Mark)
FROM Results
GROUP BY Item ITEM MIN(MARK)
-
1 55
2 65
3 40
* END OF RESULT **** * 3 ROWS DISPLAYED * What is the actual query?
Trang 1884136 - Week 7
SELECT Item,Min(Mark)
FROM Results
GROUP BY Item ITEM MIN(MARK)
-
1 55
2 65
3 40
* END OF RESULT ****
* 3 ROWS DISPLAYED *
What is the actual query?
What is the lowest mark for each assignment?
Trang 1984136 - Week 7
A similar problem
What is the highest mark for each assignment?
What key words indicate that this is a
group by problem?
Trang 2084136 - Week 7
A similar problem
What is the highest mark for each assignment?
GROUP BY… item
Trang 21It is usual to reference the group in the select statement
SELECT Item, MAX(Mark)
Trang 2284136 - Week 7
What is the highest mark for each assignment?
SELECT Item, MAX(Mark)
FROM Results GROUP BY Item
SELECT Item, MAX(Mark)
FROM Results GROUP BY Item
Trang 2484136 - Week 7
List the total mark for each student?
Another Query
Trang 25GROUP BY
Trang 2684136 - Week 7
List the total mark for each student?
Another Query
SELECT FROM
GROUP BY Id
Trang 27GROUP BY Id
Trang 29GROUP BY Id
Trang 30Id is used for more useful output
statistical function
Trang 3184136 - Week 7 ITEM ID SUBMITTED MARK
Trang 3284136 - Week 7 ITEM ID SUBMITTED MARK
Trang 3384136 - Week 7 ITEM ID SUBMITTED MARK
SELECT ID, SUM(Mark)
FROM Results
GROUP BY ID
Trang 3484136 - Week 7
ID SUM(MARK) - -
Trang 3584136 - Week 7
used when more than one table needs to
be joined to find all information
Example :
Find the average mark of each
assignment and provide a description of each assignment
Trang 361 Which words indicate a group by situation?
Average mark - statistical function Each assignment - each group
avg(Mark)
Trang 3784136 - Week 7
Average mark each assignment assignment names
2: Match these key words
Find the average mark of each assignment and name each assignment.
Find the average mark of each assignment and name each assignment.
Column Tables
… … …
Trang 3884136 - Week 7
Average mark each assignment assignment names
2: Match these key words
Find the average mark of each assignment and name each assignment.
Find the average mark of each assignment and name each assignment.
Trang 4184136 - Week 7
Step 4:
Each assignment is used in the group by clause
Average mark each assignment assignment names
Find the average mark of
name each assignment.
name each assignment.
Column Tables Mark Results Item Results, Assess Description Assess
Trang 4284136 - Week 7
Step 4:
Each assignment is used in the group by clause
Average mark each assignment assignment names
Find the average mark of
name each assignment.
name each assignment.
Column Tables Mark Results Item Results, Assess Description Assess
each assignment Item Results, Assess GROUP BY Item (from results table
Trang 4384136 - Week 7
Find the average mark of
name each assignment.
name each assignment.
Step 4:
Each assignment is used in the group by clause
BUT: Choose only one table Group by r.Item or
Group by a.Item (r = Results table, a = Assess table)
Trang 4484136 - Week 7
Find the
name each assignment.
name each assignment.
Step 5:
Statistical function
1 Average Mark = AVG(Mark)
Trang 4584136 - Week 7
Find the
Step 5:
Statistical function
1 Average Mark = AVG(Mark)
2 Writing the description = MAX(Description) (necessary only for group by problems)
Trang 46FROM Results r, Assess A
WHERE r.Item = a.Item
GROUP BY r.Item
Trang 47WHERE r.Item = a.Item
GROUP BY r.Item
Trang 51SELECT a.Id, a.Mark, b.Mark
FROM Results a, Results b
WHERE a.Id = b.Id
AND a.Item = 1
AND b.Item = 2
ORDER BY 1
Trang 5284136 - Week 7
Meaning of each line of Code
SELECT a.Id, a.Mark, b.Mark
FROM Results a, Results b
WHERE a.Id = b.Id
assignment 1 for first table assignment 2 for second table
all rows are ordered by column 1
ie the Id’s
Trang 5384136 - Week 7
Output of Similar Code
SELECT a.Id, a.Mark, b.Mark
FROM Results a, Results b
WHERE a.Id = b.Id
Trang 5784136 - Week 7
Process: Query Nesting
Inner query finds the first result structure
SELECT
FROM
WHERE = (SELECT MIN(Mark) INNER QUERY
FROM Results WHERE item = 1)
Trang 5884136 - Week 7
Process: Query Nesting
Outer Query uses this result structure
FROM Results
FROM Results WHERE item = 1)
Trang 59FROM Results WHERE item = 1)
Trang 60From Results Where Item = 2))
Trang 61From Results Where Item = 2))
Trang 62From Results Where Item = 2))
Trang 63From Results Where Item = 2))
Trang 64From Results Where Item = 2)) Step 1.
Trang 65From Results Where Item = 2))
Trang 66From Results Where Item = 2)) Step 2.
Trang 6784136 - Week 7
What is the name of the student who got the highest mark in the second assignment?
Select First,Last From Students Where Id = (Select Id
Where item = 2 And mark = (Select Max(Mark) From Results
Where Item = 2))
Trang 68where item = 2 and mark = (select Max(Mark) From Results
Where Item = 2))
Trang 6984136 - Week 7
What is the name of the student who got the highest mark in the second assignment?
Select First,Last From Students where Id = (Select Id from Results where item = 2 and mark = (select Max(Mark) From Results
Where Item = 2))
FIRST LAST - - Rip Orff
Trang 70language: SQL
Trang 71r.mark > = 50 r.id}
implementation:
Select id from results where item = 1 and mark >= 50
An Example
Trang 73Informal text List the student id and mark for assignment 1 Formal text
{r:results| r.item = 1 r.id, r.mark}
Trang 7484136 - Week 7
Statistical Query
SQL
Select count(*) from assess
informal text How many assessment items are there? Formal text
count {a:assess }
Trang 7584136 - Week 7
Join Query
SQL Select first, last, mark from students s, results r where s.id = r.id
informat text
List the students and their marks for assignment 1.
format text
{s:students, r:results| s.id = r.id and r.item = 1
s.first, s.last , r.mark}
Trang 7784136 - Week 7
Product Query
SQL Select a.id from results a, results b where a.id = b.id
and a.item = 1 and b.item = 2 and a.mark > b.mark
Trang 78Informal text Which student attained
the best mark in assignment1
Formal text
{r:results| r.item = 1 and
Trang 7984136 - Week 7
Applications
The remaining screens will present
SQL code from Appendix 2, Reading 3,
84136 Resource book.
the output from the code, based on subject database on P 92
subject database on P 92 of Edmond text
the associated question
Trang 8284136 - Week 7
What is the actual question?
SELECT ID, MAX(MARK)
FROM RESULTS
GROUP BY ID
ORDER BY ID
ID MAX(MARK) - -
Trang 8384136 - Week 7
What is the actual question?
SELECT ID, MAX(MARK)
FROM RESULTS
GROUP BY ID
ORDER BY ID
ID MAX(MARK) - -
Trang 84
* END OF RESULT **
*** 3 ROWS
Trang 88List the output in the table, using p 92 Edmond
Trang 89Group3.dat
Trang 91For each assignment, state the minimum, maximum and average value List in assignment order.
Trang 9284136 - Week 7
Group4.dat
SELECT ID, SUM(MARK*WEIGHT/100)
FROM RESULTS R, ASSESS A
WHERE R.ITEM = A.ITEM
GROUP BY ID
ORDER BY ID
complete the table
ID SUM(MARK*WEIGHT/100)
-
Trang 93
84136 - Week 7
Group4.dat
SELECT ID, SUM(MARK*WEIGHT/100)
FROM RESULTS R, ASSESS A
WHERE R.ITEM = A.ITEM
GROUP BY ID
ORDER BY ID
ID SUM(MARK*WEIGHT/100) -
Trang 9484136 - Week 7
Group4.dat
SELECT ID, SUM(MARK*WEIGHT/100)
FROM RESULTS R, ASSESS A
WHERE R.ITEM = A.ITEM
GROUP BY ID
ORDER BY ID
ID SUM(MARK*WEIGHT/100) -
Trang 9584136 - Week 7
Group4.dat
SELECT ID, SUM(MARK*WEIGHT/100)
FROM RESULTS R, ASSESS A
WHERE R.ITEM = A.ITEM
GROUP BY ID
ORDER BY ID
ID SUM(MARK*WEIGHT/100) -
For each student,
list their overall mark
Trang 9684136 - Week 7
SELECT R.ID, MAX(FIRST),MAX(LAST), SUM(MARK*WEIGHT/100)
FROM STUDENTS S, RESULTS R, ASSESS A WHERE S.ID = R.ID
AND R.ITEM = A.ITEM GROUP BY R.ID
ORDER BY R.ID complete the table
R.ID MAX(FIRST) MAX(LAST) SUM(MARK*WEIGHT/100)
- -
-
Group5.dat