Sei sulla pagina 1di 1

Chapter 3: Selecting 77

B 1 1 NULL NULL 4 333 100 4000 1 333


-------------------------------------------------------------------------
R3 NULL NULL NULL NULL NULL NULL 200 NULL 6 1998
-------------------------------------------------------------------------
E 2 2 2 21 3 333 200 3000 3 999
F 2 2 2 22 3 333 200 3000 3 999
G 2 2 2 23 3 333 200 3000 3 999
-------------------------------------------------------------------------
H 2 2 2 21 4 333 200 4000 3 999
I 2 2 2 22 4 333 200 4000 3 999
J 2 2 2 23 4 333 200 4000 3 999

Note: The ROLLUP summarization of statistical functions like AVG work on


the individual rows rather than the groups. This does make a difference, and its
one of the reasons the groups still contain the separate rows in this explanation.
For example, if the SELECT list had contained AVG ( t1.key_1 ) the ROLLUP
grand total row would contain 1.75, which is the average of t1.key_1 across all
eight rows. Thats the right answer; wrong answers include 1.5, which is the
average of t1.key_1 for the four groups, and 4, which is the average of t1.key_1
for the two ROLLUP subtotal rows.

Step 7: All GROUPING calls are evaluated for each group and appended to
each row in each group. This example doesnt have any GROUPING calls, but
if it did, each call would result in yet another item appended to each row con-
taining a 1 or a 0. The GROUPING function is described in Section 3.15,
GROUP BY ROLLUP Clause.
Step 8: The HAVING clause is applied to eliminate entire groups.
In this case, the HAVING COUNT(*) > 1 clause eliminates the two sin-
gle-row groups where a = 100, and that means the candidate result set no longer
adds up to the subtotal R2 or the grand total R1:
t1. t1. t2. t2. t3. t3. COUNT SUM
key_1 non_key_1 key_1 key_2 key_1 non_key_1 a b AS c AS d
===== ========= ===== ===== ===== ========= === ==== ===== ====
R1 NULL NULL NULL NULL NULL NULL NULL NULL 8 2664
-------------------------------------------------------------------------
R2 NULL NULL NULL NULL NULL NULL 100 NULL 2 666
-------------------------------------------------------------------------
R3 NULL NULL NULL NULL NULL NULL 200 NULL 6 1998
-------------------------------------------------------------------------
E 2 2 2 21 3 333 200 3000 3 999
F 2 2 2 22 3 333 200 3000 3 999
G 2 2 2 23 3 333 200 3000 3 999
-------------------------------------------------------------------------
H 2 2 2 21 4 333 200 4000 3 999
I 2 2 2 22 4 333 200 4000 3 999
J 2 2 2 23 4 333 200 4000 3 999
The HAVING COUNT(*) > 1 clause doesnt affect any of the ROLLUP sum-
mary rows in this example even though they appear as single-row groups; thats
because COUNT(*) returns 8, 2, and 6 for each of the ROLLUP rows labeled
R1, R2, and R3, respectively. In other words, COUNT(*) for a ROLLUP row
counts the number of rows that the ROLLUP row represents. If the HAVING
clause had specified COUNT(*) > 2 instead, the ROLLUP row labeled R2
would have been eliminated at this point.

Potrebbero piacerti anche