Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nov 2001 cylauFoxPro Chapter 101 Chapter 10 Conditions, Aggregate Functions and Groups.

Similar presentations


Presentation on theme: "Nov 2001 cylauFoxPro Chapter 101 Chapter 10 Conditions, Aggregate Functions and Groups."— Presentation transcript:

1 Nov 2001 cylauFoxPro Chapter 101 Chapter 10 Conditions, Aggregate Functions and Groups

2 Nov 2001 cylauFoxPro Chapter 102 A Bigger Subset of SQL SELECT somefields FROM tableName [WHERE filter condition] [GROUP BY groupColumn] [HAVING aggregate filterCondition] [ORDER BY orderItem] [TO somewhere | INTO TABLE tablename]

3 Nov 2001 cylauFoxPro Chapter 103 The Where Clause zthe WHERE clause select particular rows from a table SELECT * ; FROM student ; WHERE house = “Hope” 7S 38WongHope13.1

4 Nov 2001 cylauFoxPro Chapter 104 Filter Conditions zRelational Operators =, <>, >, >=, <, <= zScope Operators IN,BETWEEN … AND …,LIKE zLogical Operators NOT,AND,OR

5 Nov 2001 cylauFoxPro Chapter 105 Evaluation of “=“ zSET EXACT ON|OFF has no effect zIn SQL, SET ANSI ON|OFF has similar effect zSET ANSI OFF (default) yCompared up to the last character of the shorter string. zSET ANSI ON yCompared up to the last character of the longer string. The shorter string is padded with blank characters

6 Nov 2001 cylauFoxPro Chapter 106 Evaluation of “=“ (2) SET ANSI OFF SELECT * FROM student ; WHERE name = “Tom” zShows all records with names begin with “Tom’’ xe.g. name is “Tommy” && “Tommy” = “Tom” SET ANSI ON SELECT * FROM student ; WHERE name = “Tom”&& “Tommy” <> “Tom ” zShows only records with names exactly equal “Tom”

7 Nov 2001 cylauFoxPro Chapter 107 IN SELECT name FROM student WHERE house IN (‘Faith’, ‘Wisdom’) SELECT name FROM table WHERE dob IN ({12/25/80},{12/25/81})

8 Nov 2001 cylauFoxPro Chapter 108 BETWEEN … AND... SELECT name ; FROM student ; WHERE dob ; BETWEEN {07/01/80} AND {06/30/81} zCan be used in values, string and date zInclusive zBETWEEN e1 and e2 && e1 <= e2 ye.g. dob BETWEEN {06/30/81} AND {07/01/80} will evaluate to.F. for all records because e1 > e2

9 Nov 2001 cylauFoxPro Chapter 109 BETWEEN … AND … (2) STUDENT ClassName 7AIdy 7AJanet 7AKitty 7SOlivia SELECT *; FROM student; WHERE name BETWEEN ‘I’ AND ‘K’ ySET ANSI OFFIdy, Janet and Kitty (Kitty = K) ySET ANSI ONIdy and Janet

10 Nov 2001 cylauFoxPro Chapter 1010 Operator LIKE zFor character data only zCompares skeletons of two strings zWildcards ysingle character wildcard : “_” xexactly one character x“_BC” matches “ABC” but not “BABC” nor “BC” yarbitrary character wildcard : “%” xzero to any number of characters x“%BC” matches “BC”, “aabbBC” but not “ABbC”

11 Nov 2001 cylauFoxPro Chapter 1011 Operator LIKE (2) SELECT name; FROM atable; WHERE name LIKE “%NG%” zThis will match “CHEUNG”, “NGAI”, “NG”, “SONGS” etc... zWhich will match “C_mp_t%” ? Computer

12 Nov 2001 cylauFoxPro Chapter 1012 Aggregate Functions zAggregate functions calculates across many records and arrive at one result per table (or per group) zMany-to-one functions (many rows to get one) zExamples : COUNT(), SUM(), AVG(), MAX(), MIN() zTheir effects are table-wise or group-wise (the concept of groups will be introduced soon.) zResult is a single row for a table or each group

13 Nov 2001 cylauFoxPro Chapter 1013 COUNT() zSELECT COUNT(*) FROM student (SELECT * FROM student, then count the number of records) 4 zSELECT COUNT(name) FROM student (SELECT name FROM student, then count the no. of records) 4 zSELECT COUNT(name) FROM student; WHEREclass = “7A” (SELECT name FROM student WHERE class = “7A”, then count the no. of records) 3 classname 7AIdy 7AJanet 7AKitty 7SOlivia

14 Nov 2001 cylauFoxPro Chapter 1014 COUNT() SELECT name, COUNT(name) FROM student nameCNT Olivia4 zShow only one line: name field of the last record, and the name count of the entire table (Confusing, 4 Olivia ?) zFields which are not related to the aggregate function, here name, should not be used zPlacing an aggregate function next to a non-related field is not allowed in standard SQL (but ok in FoxPro)

15 Nov 2001 cylauFoxPro Chapter 1015 Points to Note for Aggregate functions zAfter the execution of an aggregate function, the file pointer is points to the top of the selected records within the cursor. zWhen it is a table-wise operation (no use of WHERE), the file pointer points to the last record in the table.

16 Nov 2001 cylauFoxPro Chapter 1016 MAX(), MIN() STUDENT NameMarkdobsex Chan90{05/17/84}M Lee60{11/12/83}F Cheung70{01/01/85}M Wong80{07/11/83}F ySELECT MAX(mark) FROM student 90 ySELECT MIN(name), MAX(dob); FROM student WHERE sex = “M” Chan {01/01/85}

17 Nov 2001 cylauFoxPro Chapter 1017 SUM(), AVG() zSELECT SUM(mark) FROM student sum_mark 300 zSELECT AVG(mark) FROM student WHERE sex = “M” avg_mark 80 zThe functions SUM() and AVG() only work on numeric types

18 Nov 2001 cylauFoxPro Chapter 1018 GROUP BY SELECT * FROM student GROUP BY sex Wong80{07/11/83}F Cheung70{01/01/85}M zThese two keywords summarize a long table into a short cursor with 2 records (2 groups) zOnly the last record of each group shows up in the cursor. zWithin a group, if different records have different values for a field (here name), then it will be misleading if the value of the last record (Wong, Cheung) in the group is listed to represent that field.

19 Nov 2001 cylauFoxPro Chapter 1019 GROUP BY (2) zThese two keywords ‘’Group By” are only good for aggregate functions and the field used as grouping criteria. zSorting as a by-product SELECT sex, SUM(mark), MAX(mark); FROM student ; GROUP BY sex TO SCREEN sexsum_markmax_mark F140 80 M16090

20 Nov 2001 cylauFoxPro Chapter 1020 GROUP BY (3) SELECT count(*), YEAR(dob) FROM student ; GROUP BY YEAR(dob) zIt will fail, should be: SELECT count(*), YEAR(dob) AS yr FROM student; GROUP BY yr CNT yr 21983 11984 11985

21 Nov 2001 cylauFoxPro Chapter 1021 GROUP BY (4) zMultiple field indexing is supported. SELECT class, house, count(*) FROM student GROUP BY class, house zModifying GROUP BY with ORDER BY SELECT department, AVG(salary); FROM staffrec; GROUP BY department; ORDER BY name

22 Nov 2001 cylauFoxPro Chapter 1022 Pre-filtering with WHERE before applying GROUP BY SELECT sex, COUNT(*) as number FROM student WHERE name LIKE “%n%” GROUP BY sex sexnumber F1 M2 zYou should place WHERE before GROUP BY zInternally the WHERE filtering is performed before GROUPing. NameMarkdob sex Chan90{05/17/84} M Lee60{11/12/83} F Cheung70{01/01/85} M Wong80{07/11/83} F

23 Nov 2001 cylauFoxPro Chapter 1023 Using Aggregate function in WHERE SELECT sex FROM student WHERE AVG(mark) > 70 GROUP BY sex zThis will fail (error message: avg.prg does not exist) zWHERE can only handle data that is immediately available zAggregate functions are not allowed within the WHERE clause

24 Nov 2001 cylauFoxPro Chapter 1024 The HAVING Clause zSQL provide the HAVING clause which is similar to but not exactly equal to the WHERE clause SELECT sex FROM student; GROUP BY sex; HAVING AVG(mark) > 70; sex M zThe HAVING filtering is executed after GROUPing. zAfter grouping, each record (group) has one single value of AVG(mark) so it can be compared against 70

25 Nov 2001 cylauFoxPro Chapter 1025 HAVING vs WHERE zThe difference is a matter of time ordering: The order of filtering: 1. WHERE 2. GROUP BY (evaluation of aggregate functions) 3. HAVING zWHERE is checked before the AVG() is evaluated. HAVING is checked after. zWHERE is checked before grouping is done. HAVING is checked after. zSo for aggregate functions with GROUP BY, WHERE will fail and HAVING will succeed.

26 Nov 2001 cylauFoxPro Chapter 1026 Don’t use HAVING other than cases with GROUP BY SELECT name FROM student HAVING mark < AVG(mark) zThis is supposed to find the names of students with marks lower than average. zThe SELECT part gives only the last record (after calculating AVG(mark)), not one record for each group, for HAVING to choose from. zIt depends on the last record: if the last record satisfies the HAVING clause, then the “correct result” is shown, otherwise no results is shown.( A matter of luck)

27 Nov 2001 cylauFoxPro Chapter 1027 Aggregate function without GROUP BY zTo find those students with marks lower than average, you should use subquery (to be covered in chapter 12) SELECT name from student; WHERE mark<(SELECT AVG(mark) from student) name Lee Cheung

28 Nov 2001 cylauFoxPro Chapter 1028 HAVING vs. WHERE (2) zIf aggregate functions are not involved, HAVING and WHERE give the same response. zIf an aggregate function is involved: yWHERE cannot handle (Why?) yWith GROUP BY: use HAVING yWithout GROUP BY: (Don’t use HAVING) xThe aggregate function in HAVING will trigger an overall evaluation (although in the absence of GROUP BY), leaving the last record in the field buffers, so all further filtering concerns that last record only. Usually that is not what you want. xUse subquery (Chapter 12) zSo, use HAVING with GROUP BY only.

29 Nov 2001 cylauFoxPro Chapter 1029 Combining HAVING with WHERE SELECT sex, AVG(mark) FROM student WHERE name like ‘%n%’ GROUP BY sex HAVING AVG(mark) > 70 TO SCREEN sexavg_mark F80.00 M80

30 Nov 2001 cylauFoxPro Chapter 1030 Exercise zChapter 10 Q.7, Q.8, Q.9, 14 zFurther Practice FP 2 Q.1 FP 4 Q.3, 4 FP 6 Q.1, 2 FP 7 Q.4, 5 FP 11 Q.2, 3, 4 FP 15 Q.1, 2, 3


Download ppt "Nov 2001 cylauFoxPro Chapter 101 Chapter 10 Conditions, Aggregate Functions and Groups."

Similar presentations


Ads by Google