Download presentation
Presentation is loading. Please wait.
1
Group Operations Part IV
2
Aggregate Functions Summarizes the results of a query over a number of rows, returning a single value. aggregate_function([DISTINCT | ALL] expression) aggregate_function: The name of the function DISTINCT: Consider only unique values from expression ALL: Consider all values, including duplicates, from expression expression: Specifies a column or other query on which to perform the aggregation
3
Aggregate Functions SUM COUNT AVG MAX MIN
Returns the total of a numeric column or expression COUNT Returns the number of rows matching the criteria of the expression AVG Returns the average of a numeric column or expression MAX Returns the largest value from a column or expression MIN Returns the smallest value from a column or expression
4
Effect of NULL Values Aggregate functions, except COUNT(*), ignore NULLs – COUNT(*) counts rows, not values SELECT COUNT(*) addresses 3,213,097 ,COUNT(spraddr_to_date) to_dates 1,479,767 FROM spraddr SELECT AVG(ssbsect_bill_hrs) FROM ssbsect WHERE ssbsect_subj_code = 'ACT' AND ssbsect_term_code = '201990' SELECT AVG(NVL(ssbsect_bill_hrs, 0))
5
Group By Combines a result set into multiple groups and produces a single row of summary data for each group. SELECT c.scbcrse_coll_code college ,COUNT(*) courses FROM scbcrse c WHERE c.scbcrse_eff_term = (SELECT MAX(cx.scbcrse_eff_term) FROM scbcrse cx WHERE cx.scbcrse_subj_code = c.scbcrse_subj_code AND cx.scbcrse_crse_numb = c.scbcrse_crse_numb AND csus_f_cur_term() >= cx.scbcrse_eff_term) AND c.scbcrse_csta_code = 'A' GROUP BY c.scbcrse_coll_code ORDER BY college
6
Group By Results
7
Group By Columns or expressions contained in the SELECT clause that includes an aggregate function must also be included in the GROUP BY clause. SELECT c.scbcrse_coll_code college ,COUNT(*) courses FROM scbcrse c ORDER BY college
8
Having The HAVING clause defines a filter for the GROUP BY clause.
WHERE : SELECT as HAVING : GROUP BY SELECT c.scbcrse_coll_code college ,c.scbcrse_credit_hr_low low_credits ,COUNT(*) courses FROM scbcrse c WHERE c.scbcrse_eff_term = (SELECT MAX(cx.scbcrse_eff_term) FROM scbcrse cx WHERE cx.scbcrse_subj_code = c.scbcrse_subj_code AND cx.scbcrse_crse_numb = c.scbcrse_crse_numb AND csus_f_cur_term() >= cx.scbcrse_eff_term) AND c.scbcrse_csta_code = 'A' GROUP BY c.scbcrse_coll_code ,c.scbcrse_credit_hr_low HAVING c.scbcrse_credit_hr_low > 5 ORDER BY college
9
HAVING Results w/o HAVING Results w/ HAVING
10
Having The HAVING clause may only refer to expressions in the SELECT list, or to an expression involving an aggregate function SELECT c.scbcrse_coll_code college ,COUNT(*) courses FROM scbcrse c WHERE c.scbcrse_eff_term = (SELECT MAX(cx.scbcrse_eff_term) FROM scbcrse cx WHERE cx.scbcrse_subj_code = c.scbcrse_subj_code AND cx.scbcrse_crse_numb = c.scbcrse_crse_numb AND csus_f_cur_term() >= cx.scbcrse_eff_term) AND c.scbcrse_csta_code = 'A' GROUP BY c.scbcrse_coll_code HAVING COUNT(*) > 700 ORDER BY college
11
Having Results w/o HAVING Results w/ HAVING
12
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.