List of articles   Choose language


Aggregates instead of recursion


Examples

Aggregates are written after service word 'where' to apply them as predicates to several sections of tree.

update p/q/r/a.b[@b1 as @m]*.c[@c1 as @n]*          -- rational tree
  set    @a1=5,  @b2=10
  where  sum(@m)+sum(@n)>1;
update tab/@fld/p/q/r/a.b[@b1 as @m]*.c[@c1 as @n]* -- xml-tree
  set    @a1=5,  @b2=10
  where  sum(@m)+sum(@n)>1;
or after service word 'select'
select sum(@b1) as @m, sum(@c1) as @n
  from a.b*.c* ;

Comparisons

For example, to find the most cheap path between cities

create table city (
  id   number  primary key,
  name varchar
);
create table price (
  id1  number  references city (id),
  id2  number  references city (id),
  cost money
);
we could write laconically and clearly, using XTree and virtual foreign key (p. 21 and 25 of pdf-document)
select sum(@cost)
from   city[@id=5].price*.city[@id=700]
where  previous(price)/@id2=next(price)/@id1;
instead of bulky expression
with recursive res (@id1, @id2, @total) as (
  select  @id1, @id2, @cost
    from  price
    where @id1=5
  union
  select  res/@id1, price/@id2, res/@total+price/@cost
    from  res, price
    where res/@id2=price/@id1
) select  @id1, @id2, @total
    from  res
    where @id2=700;
And even
select min( sum(@cost) )
from   city[@id=5].price*.city[@id=700]
where  previous(price)/@id2=next(price)/@id1;
instead of
with recursive res (@id1, @id2, @total) as (
  select  @id1, @id2, @cost
    from  price
    where @id1=5
  union
  select  res/@id1, price/@id2, res/@total+price/@cost
    from  res, price
    where res/@id2=price/@id1
) select  @id1, @id2, min(@total)
    from  res
    where @id2=700;

P.S.

Article is guide to implement ideas of p.52-70 of pdf-document.



Dima Turin, dmitryturin@yandex.ru



List of articles   Choose language


Используются технологии uCoz