List of articles   Terminology   Choose language


Examples


Example 1. Select employee, salary of which above average salary in their department, in order of decreasing of their salary.

create table department {
  id         num      primary key,
  name       string
};
create table employee {
  id         num      primary key,
  name       string,
  salary     num,
  department num      references department (id)
};
Request is on the left below, output is on the right below (field "department" of table "employee" of is not in output, because it is already used during creating output).
select department.employee
  where @salary > (
    select avg(@salary)
    from   employee e
    where  e/@department=department/@id
  ) order by @salary desc;
<department id="1"  name="Technical">
  <employee id="54" name="Fraud"   salary="3200">
  <employee id="72" name="Kitter"  salary="3100">
  <employee id="31" name="Tomson"  salary="3000">
</department>
<department id="2"  name="Marketting">
  <employee id="25" name="Johnson" salary="4100">
  <employee id="64" name="Smith"   salary="4000">
</department>

Example 2. Let there are a set of cities and set of flights between them.

create table city (
  id     num primary key,
  name   string
);
create table flight (
  c1     num references city (id),
  c2     num references city (id),
  t1     time,
  t2     time,
  day    weekdays
);

Example 3. Let we search path in graph, going through minimum quantity of nodes. Each node is record in table "point". Bonds between nodes is in table "bond". Path is searched from node, primary key of which is in field "tab.from" (initial node), to node, primary key of which is in field "tab.to" (final node), table "tab" has one record.

create table point (
  id     num primary key,
  moment num
);
create table bond (
  p1     num references point (id),
  p2     num references point (id)
);
create table tab (
  from   num references point (id),
  to     num references point (id)
);

Let's look at all nodes from initial node to final node - wave goes. Each node in field "moment" contains moment of receipts of wave (moment is equal to double quantity of gone nodes). We find all the most short (with identical length) pathes in graph (if pathes exist).

update point set @moment=null;
update tab#from.point.(bond#p1:p2.point[@moment=null])*.tab#to
  set  @moment=!
;
if           tab#to.point/@moment is not null
then select  point.(point)*
       from  tab#from.point.(bond#p1:p2.point[@moment=!])*.tab#to
else select "<p>Pass does not exist</p>"
;
<point    id="3">
 <point   id="4"
  ...
   <point id="11"
  ...
 </point>
</point>

Example 4. Each node, referring to other node, specifies weight of bond to new node (in field "weight"). It's required to find such path in graph, for which sum of weights is minimal. It's supposed, that contour with negative total weight don't exist in graph (such contour allow to make weight of path as small as you want, "having scrolled" in contour necessary quantity of time. For example, weightab=weightba in symmetric graph, any bond with negative weight is such contour).

create table point (
  id     num primary key,
  moment num,
  total  float4
);
create table bond (
  p1     num references point (id),
  p2     num references point (id),
  weight float4
);
create table tab (
  from   num references point (id),
  to     num references point (id)
);
We search pass
update point set @moment=null, @total=null;
update tab#from.point.(bond#p1:p2.point[@moment=null or @total>../../@total+./@weight])*.tab#to
  set  @moment=!, @total=../../@total+./@weight
;
if           tab#to.point/@moment is not null
then select  point.(point)*
       from  tab#from.point.(bond#p1:p2.point[@moment=! and @total=../../@total+./@weight])*.tab#to
else select "<p>Pass does not exist</p>"
;

Dmitry Turin



List of articles   Terminology   Choose language