Download presentation
Presentation is loading. Please wait.
1
Advanced Summary Queries CIT 381 2001 October 24
2
GROUP BY % Find the average and total order size for each Sales Rep. % First find the order amounts of each Sales Rep. SELECT s.name, o.amount FROM salesreps s, orders o WHERE s.empl_num=o.rep ORDER BY 1; nameamount Bill Adams $3,745.00 Bill Adams $27,500.00 Bill Adams $702.00 Bill Adams $3,276.00 Bill Adams $4,104.00 Dan Roberts $22,500.00 Dan Roberts $150.00 Dan Roberts $3,978.00 Larry Fitch $7,100.00 Larry Fitch $652.00 Larry Fitch $760.00 Larry Fitch $45,000.00 Larry Fitch $776.00 Larry Fitch $1,420.00 Larry Fitch $2,925.00 Mary Jones $5,625.00 Mary Jones $1,480.00 Nancy Angelli $652.00 Nancy Angelli $31,350.00 Nancy Angelli $2,430.00 Paul Cruz $2,100.00 Paul Cruz $600.00 Sam Clark $1,458.00 Sam Clark $31,500.00 Sue Smith $2,130.00 Sue Smith $3,750.00 Sue Smith $1,896.00 Sue Smith $15,000.00 Tom Snyder $22,500.00 Tom Snyder $632.00
3
Apply Function to Each Group SELECT s.name, AVG(o.amount) as AvgSales, SUM(o.amount) AS TotalSales FROM salesreps s, orders o WHERE s.empl_num=o.rep GROUP BY s.name; nameAvgSalesTotalSales Bill Adams$7,865.40$39,327.00 Dan Roberts$8,876.00$26,628.00 Larry Fitch$8,376.14$58,633.00 Mary Jones$3,552.50$7,105.00 Nancy Angelli$11,477.33$34,432.00 Paul Cruz$1,350.00$2,700.00 Sam Clark$16,479.00$32,958.00 Sue Smith$5,694.00$22,776.00 Tom Snyder$11,566.00$23,132.00 AVG and SUM were applied to each group with same name.
4
Using Outer Join to Count Zeroes % Find the number of orders of each company (including those who have ordered zero). % First, let’s think how an outer join can list each company’s orders. SELECT c.company, o.order_num FROM customers c LEFT JOIN orders o ON c.cust_num=o.cust ORDER BY 1;
5
companyorder_num AAA Associates Ace International113034 Ace International113036 Acme Mfg.112963 Acme Mfg. 112983 Acme Mfg. 112987 Acme Mfg. 113027 Carter & Sons Chen Associates 113069 First Corp. 112968 Fred Lewis Corp. 112993 Fred Lewis Corp.113065 Holm & Landis113003 Holm & Landis113055 Holm & Landis113058 Ian & Schmidt113042 J.P. Sinclair112961 JCP Inc.112975 JCP Inc.113057 JCP Inc.113012 Jones Mfg.112989 Midwest Systems112992 Midwest Systems113051 Midwest Systems113013 Midwest Systems113049 Orion Corp.113024 Orion Corp.112979 Peter Brothers113062 Peter Brothers112997 QMA Assoc. Rico Enterprises113048 Smithson Corp. Solomon Inc. Three-Way Lines Zetacorp113045 Zetacorp113007 Notice the “groups”, from the ORDER BY
6
companyExpr1001 AAA Associates1 Ace International2 Acme Mfg.4 Carter & Sons1 Chen Associates1 First Corp.1 Fred Lewis Corp.2 Holm & Landis3 Ian & Schmidt1 J.P. Sinclair1 JCP Inc.3 Jones Mfg.1 Midwest Systems4 Orion Corp.2 Peter Brothers2 QMA Assoc.1 Rico Enterprises1 Smithson Corp.1 Solomon Inc.1 Three-Way Lines1 Zetacorp2 SELECT c.company, count(*) FROM customers c LEFT JOIN orders o ON c.cust_num=o.cust GROUP BY c.company; This mis-counts those who ordered Nothing (such as AAA)
7
companyNumOrders AAA Associates0 Ace International2 Acme Mfg.4 Carter & Sons0 Chen Associates1 First Corp.1 Fred Lewis Corp.2 Holm & Landis3 Ian & Schmidt1 J.P. Sinclair1 JCP Inc.3 Jones Mfg.1 Midwest Systems4 Orion Corp.2 Peter Brothers2 QMA Assoc.0 Rico Enterprises1 Smithson Corp.0 Solomon Inc.0 Three-Way Lines0 Zetacorp2 SELECT c.company, count(o.order_num) AS NumOrders FROM customers c LEFT JOIN orders o ON c.cust_num=o.cust GROUP BY c.company;
8
HAVING Clause A HAVING clause allows one to apply a condition to the results of the aggregate Remember order of computation FROM, pushes cross-product of tables into WHERE, surviving rows go to GROUP BY, forming groups on which aggregates are computed, from which HAVING picks out desired rows (like WHERE), then we SELECT desired attributes
9
Example of HAVING % Get the average and total sales of those Reps with over $30k in total sales. SELECT s.name, AVG(o.amount) as AvgSales, SUM(o.amount) AS TotalSales FROM salesreps s, orders o WHERE s.empl_num=o.rep GROUP BY s.name HAVING SUM(o.amount)>=30000; nameAvgSalesTotalSales Bill Adams$7,865.40$39,327.00 Larry Fitch$8,376.14$58,633.00 Nancy Angelli$11,477.33$34,432.00 Sam Clark$16,479.00$32,958.00
10
More Examples from SQL Text % For each office with two or more people, compute the total quota and total sales for all salespeople who work in the office. SELECT city, sum(quota), sum(salesreps.sales) FROM offices, salesreps WHERE office=rep_office GROUP BY city HAVING count(*)>=2; cityExpr1001Expr1002 Chicago$775,000.00$735,042.00 Los Angeles$700,000.00$835,915.00 New York$575,000.00$692,637.00
11
% Show the price, quantity on hand, and total quantity on order for each product where the total quantity on order is more than 75 per cent of the quantity on hand. SELECT description, price, qty_on_hand, sum(qty) FROM products, orders WHERE mfr=mfr_id AND product=product_id GROUP BY mfr_id, product_id, description, price, qty_on_hand HAVING SUM(qty) > (.75*qty_on_hand) ORDER BY 3 DESC;
12
descriptionpriceqty_on_handExpr1003 Reducer$355.003832 Widget Adjuster$25.003730 Motor Mount$243.001516 Right Hinge$4,500.001215 500-lb Brace$1,425.00522 Notice the “bad” style – no renaming of tables Can get away with it here – unique attribute names DESC will sort descending (ASC is default) Any attributes in SELECT clause must appear in GROUP BY clause. Result of previous query.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.