Presentation is loading. Please wait.

Presentation is loading. Please wait.

Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.

Similar presentations


Presentation on theme: "Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider."— Presentation transcript:

1 Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider the Extend operator & its implementation in SQL.

2 Example : Whole Relation as a Group EMPLOYEE GroupBy[ ] With[ Total  Bag[ Sal ] Sum ] Get the total salary bill. Total 222238,500 33E177Robson 111E3555Smith 22E566Mitchell 444E6888Blake E8Jones ENoEName E2Atkins E4Fenwick E7Watson 66D 222S 44M 888M D M-S M S D Sal 66 32,500 22218,000 4424,000 88854,000 18,000 24,000 40,000 28,000 EMPLOYEE Sal 66 32,500 22218,000 4424,000 88854,000 18,000 24,000 40,000 28,000 111 No attribute

3 Whole-Relation ‘GroupBy’ Procedure 1.Remove irrelevant attribute(s). AS USUAL. 2.Add an empty set of attributes to the operand as the grouping attributes. It does not alter the operand, since it is empty ! The empty ‘attribute’ has the same value (i.e. the empty set) in every tuple. Group on this attribute; this yields only one group. 3.Create one result tuple / group consisting of the grouping attribute. Thus there is one tuple initially with no attributes, i.e. an attribute containing the empty set. 4.Carry out aggregations. AS USUAL. This will add aggregation result attribute(s) to the one tuple.

4 SQL : Whole-Relation Group The SQL syntax for a whole-relation GroupBy is : Select “aggregation” As Result-Name FromRELATION_NAME ; Repeat this for each aggregation. The syntax of “aggregation“ remains unchanged. Example : “Get the total salary bill”. (Previous example). SelectSum( All Sal ) As Total FromEMPLOYEE ; The Group By phrase is omitted - no grouping attributes to put in it. No grouping attributes in the Select phrase.

5 After a GroupBy ? l Sometimes it is useful to carry out another operation on the result of a GroupBy. l There is no need to do a Projection on the result since it can be incorporated into the GroupBy. l To do a Restriction on the result, SQL provides a Having phrase. This is because SQL’s built-in sequence of operations has already executed all Restrictions. There is no way to do another without introducing a special phrase, i.e. the Having phrase. l The Having restriction condition is limited to using : l a grouping attribute, l an aggregation results. Because the result of a GroupBy contains nothing but these.

6 Executing an SQL ‘Select’ Statement Order By Where Group By Having From Select Restrictions done here. Another Restriction here ! Grouping done here. The phrases are executed in the following order. Joins / Cartesian Products done here. Sequencing done here. Projections done here.

7 ‘Having’ Phrase The SQL syntax for a Group By with a Having phrase is : Select GroupingAttribute(s), “aggregation” As Result_Name From RELATION_NAME Group By GroupingAttribute(s) Havingcondition ; The Having phrase must immediately follow a Group By phrase, and not be anywhere else. “condition” can contain only grouping attribute(s) and aggregate function applications in a Boolean combination of comparisons.

8 Example of the ‘Having’ Phrase (1) “Get the total salary paid to each marital-status group, for groups where the total exceeds £50,000.” Select M-S, Sum( All Sal ) As Total From EMPLOYEE Group By M-S Having Sum( Sal ) > 50000 ; EMPLOYEE GroupBy[ M-S ] With[ Total  Bag[ Sal ] Sum ] Restrict[ Total > 50000 ] A GroupBy followed by a Restriction. Cannot use the name Total.

9 Example of the ‘Having’ Phrase (2) “How many different shipment sizes are there per supplier, for suppliers who ship more than 2 shipment sizes ?” Select SNo, Count( Distinct Qty ) As Sizes From SHIP Group By SNo Having Count( Distinct Qty ) > 2 ; SHIP GroupBy[ SNo ] With[ Sizes  Project[ Qty ] Count ] Restrict[ Sizes > 2 ]

10 ‘Before’ and ‘After’ Restrictions It is possible to write algebraic expressions like : ( … expression … ) Restrict[ … ] GroupBy[.. ] With[ … ] Restrict[ … ] The equivalent in SQL is : Select … From … Where … Group By … Having … ; Before After Before

11 Example of Extend 192 0 1538 840 225 50 1000 546 EuroMaxB 100 50 0 0 O/DAmt 5 6 7 8 125 0 1000 546 10/23A 10/29C 32/14X 47/18G A/C BANK BANK Extend[ Euro  Amt/0.65 ; MaxB  Amt + O/D ] Amt 10/23A 10/29C 32/14X 47/18G A/C 5 6 7 8 125 0 1000 546 100 50 0 0 O/D Amt 100 50 0 0 5 6 7 8 125 0 1000 546 10/23A 10/29C 32/14X 47/18G A/C BANK

12 Definition of ‘Extend’ l Creates a new relation formed from its operand by adding one or more named attributes to it. l Each new attribute(s) contain values specified in a ‘scalar expression’. The expression’s type is the type of the new attribute. l An expression can contain one or more of : l a constant; l an attribute name : this yields that attribute’s value in the same tuple; l scalar functions & operators (of the relevant data type); l parentheses to control the execution sequence.

13 SQL : Extend The SQL equivalent of an Extension has the syntax :- Select RELATION_NAME.*, calculation As Result_Name From RELATION_NAME ; This yields all the relation’s existing attributes. Repeat for each new attribute. An ‘*’ on its own is only allowed if there is nothing else in the Select phrase.

14 SQL Examples l The previous example is written in SQL as follows : Select BANK.*, Amt/0.65 As Euro, Amt + O/D As MaxB From BANK ; l “In addition to the details of all bank accounts, show how much the amount in each account would be above an overdraft limit that was 5 times greater than the current overdraft limit”. Select BANK.*, Amt + (5 * O/D) As Possible From BANK ;

15 Combining Algebra Operations l SQL treats an Extension as the reverse of a Projection, by putting both in the Select phrase. l As the Select phrase is the last to be executed by SQL, then an Extension is always the last operation to be executed, in parallel with a Projection. l This applies even when there is a Group By (with / without Having). In this case, since Extend applies scalar expressions and an aggregation can be embedded in a scalar expression, the Extension comes after the GroupBy.


Download ppt "Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider."

Similar presentations


Ads by Google