15.22

Suppose you have to compute Aγsum(C)(r) as well as A,Bγsum(C)(r). Describe how to compute these together using a single sorting of r.


First we sort relation r on (A,B). That is if two tuples t1,t2r have different A values, then we use their values for attribute A to sort them. If they have the same A value, we use their values for attribute B to sort them.

Then we perform the following:

  1. pr:= address of first tuple of r
  2. Let SA be an empty set. At the end of this algorithm it will contain the result of Aγsum(C)(r).
  3. Let SAB be an empty set. At the end of this algorithm it will contain the result of A,Bγsum(C)(r).
  4. tr:= tuple to which pr points
  5. iA:=0.
  6. while (pr null)
    1. tr:= tuple to which pr points
    2. iAB:=tr[C].
    3. Set pr to point to the next tuple of r.
    4. done:=false.
    5. while (not done and pr null)
      1. $t_r’’ := $ tuple to which pr points.
      2. If (tr[A,B]=tr[A,B] )
        1. iAB:=iAB+tr[C]
        2. Set pr to point to the next tuple of r.
      3. Else
        1. done:=true
    6. SAB:=SAB(tr[A],tr[B],iAB)
    7. iA:=iA+iAB
    8. If (tr[A]tr[A] OR $pr = $ null)
      1. SA:=SA(tr[A],iA).
      2. tr:=tr
      3. iA:=0