Download presentation
Presentation is loading. Please wait.
1
CS 3630 Database Design and Implementation
2
Project Phase II Assignment 8
Due Monday, April 10, by 5 PM
3
Aggregate Functions Produce a single value from a set
Examples: Max, Min, Sum, Avg, Count
4
Aggregate functions Count Max Min Sum Avg …
5
SQL Aggregate Functions
// Max, Min, Avg, Sum Select Max(price), Min(price), Avg(price) From room; Select Max(room_no), Min(room_no) Select Avg(room_no), Sum(room_no) // No! // Room_No char(4)
6
Max/Min Dates Select Min(Date_From), Max(Date_To) From Booking;
Col Min(Date_From) Format a20 Heading 'Earlist Date_From' Col Max(Date_To) Format a20 Heading 'Latest Date_To' Select To_Char(Min(Date_From), 'Mon dd yyyy'), To_Char(Max(Date_To), 'Mon dd yyyy')
7
Max/Min Dates col "Earliest Date from" format a20
col "Latest Date To" format a20 Select To_Char(Min(Date_From), 'Mon dd yyyy') "Earliest Date from", To_Char(Max(Date_To), 'Mon dd yyyy') "Latest Date To" From Booking; -- Incorrect! Select Min(To_Char(Date_From, 'Mon dd yyyy')), Max(To_Char(Date_To, 'Mon dd yyyy'))
8
Aggregate Functions -- Count(*): number of all records
-- count(hotel_no): number of Hotel_No -- Count(unique Hotel_No): number of distinct Hotel_No Select count(*), count(hotel_no), count(unique hotel_no) From room; -- Does not count null values select count(*), count(address) From guest;
9
Aggregate Functions Select Avg(Price) From room; -- With Distinct
Select Avg(Distinct Price)
10
Aggregate Functions with Where
-- With where clause -- Avg is applied after Where clause Select Avg(Price) From room Where rtype = 'Family'; Where rtype = 'Single' Where rtype = 'Double'
11
Aggregate Functions with Where
-- With where clause -- Avg is applied after Where clause Select 'Average Price of Family rooms: ', Avg(Price) From room Where rtype = 'Family‘; Select rtype, Avg(Price) Where rtype = 'Family'; -- change rtype to Single, Double
12
One record for each group.
Group By Clause Count, Avg Select Group By Count, Avg Count, Avg One record for each group.
13
Group By Select Avg(Price) From room Group by rtype;
Select rtype, Avg(Price)
14
Group By Clause For each hotel, display Hotel_No with the number of rooms. Select Hotel_No, Count(*) From Room Group By Hotel_No; Hotel_No Room_No Price H01 R001 30 R002 35 H05 R003 40 R103 45 R101 50 Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Count H01 3 H05 2
15
What can be selected with Group By Clause
Can select Hotel_No, the group by field Can select aggregate functions on any other fields such as Max(Room_No) or Avg(Price) Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Max(Room_No) Avg(Price) H01 R103 36.67 H05 R101 45
16
What CANNOT be selected with Group By Clause
Cannot select no group by fields such as Room_No or Price ORA-00979: not a GROUP BY expression Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Room_No Price H01 R001 30 R002 35 R103 45 H05 R003 40 R101 50 Hotel_No Room_No Price H01 ? H05
17
Group By Clause ? Select Group By ? ? One record for each group!
What can be selected? Aggregate functions Group by fields
18
Group By One Record for each group
Select Hotel_no, Count(*), Max(Price) From Room Group by Hotel_No; Select Hotel_no, Room_No, rtype, Price -- not a Group By expression
19
Group by more than one field
Select Hotel_No, rtype, Max(Price) From Room Group By Hotel_no, rtype;
20
Order after Group by Group By Select Hotel_no, Count(*), Max(Price)
From Room Group by Hotel_No Order by Count(*);
21
Group By Clause ? Select Group By ? ?
What if we don’t want all groups? Having on groups Where on records
22
Group By Having to select groups Select Hotel_no, Count(*), Max(Price)
From Room Group by Hotel_No Having Count(*) > 20;
23
Group By Where before Group by to select records
Having after Group by to select groups Let’s do it in 4 steps: From, Where, Group By, Having Select Hotel_no, Count(*), Max(Price) From Room Where rtype = 'Single' Group by Hotel_No Having Max(Price) < 100
24
Group By Where before Group by Having after Group by Order by last
Select Hotel_no, Count(*), Max(Price) From Room Where rtype in ('Double', 'Single') Group by Hotel_No Having Count(*) > 50 Order by Count(*);
25
Group By Clause Execution Order Where (condition on records) Group by
Having (condition on groups) Order by (Select)
26
Command Prompt Prompt Prompt 9.
Prompt For each hotel that has more than Prompt one Single room, list hotel number Prompt with number of Single rooms the hotel Prompt has and the highest price of Prompt such rooms of the hotel. Select Hotel_no, Count(*), Max(Price) From Room Where rtype = 'Single' Group by Hotel_No Having Count(*) > 1 Order by Count(*);
27
Assignment 8 Due Monday, April 10 Should work for any table instance
Run against my tables Should insert more records to test (Don’t include insert commands!) Example Quiz 3 Hands-On Wednesday, April 7 Create Script file to Drop tables Create tables Column/Table constraints Insert records
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.