We start with loading a sample database. Our sample database is derived from the dataset of all employees of the city of Chicago (source).
In [1]:
include("../citydb.jl")
using RBT
setdb(citydb)
Out[1]:
We can execute a query using @query()
command:
In [2]:
@query(6*(3+4))
Out[2]:
In [3]:
@query(department.name)
Out[3]:
Find the names of all employees.
In [4]:
@query(department.employee.name)
Out[4]:
We are not longer restricted by the hierarchical structure of the database, so we can query employees directly.
In [5]:
@query(employee.name)
Out[5]:
If the traversal ends at an entity class, an array of records is generated.
In [6]:
@query(employee)
Out[6]:
In [7]:
@query(count(department))
Out[7]:
Find the number of employees for each department.
In [8]:
@query(department.count(employee))
Out[8]:
Find the total number of employees.
In [9]:
@query(count(department.employee))
Out[9]:
Again, we can query employee
directly.
In [10]:
@query(count(employee))
Out[10]:
Find the top salary among all employees.
In [11]:
@query(max(employee.salary))
Out[11]:
Find the maximum number of employees per department.
In [12]:
@query(max(department.count(employee)))
Out[12]:
In [13]:
@query(department:select(name,count(employee)))
Out[13]:
It is easy to add new columns to the output. Let us add the top salary per department.
In [14]:
@query(
department
:select(
name,
count(employee),
max(employee.salary)))
Out[14]:
In [15]:
@query(
employee
:filter(salary>200000))
Out[15]:
Find the departments with more than 1000 employees.
In [16]:
@query(
department
:filter(count(employee)>1000)
:select(name, count(employee)))
Out[16]:
Find the number of departments with more than 1000 employees.
In [17]:
@query(
count(
department
:filter(count(employee)>1000)))
Out[17]:
For each department, find the number of employees with salary higher than $100k.
In [18]:
@query(
department
:select(
name,
count(employee:filter(salary>100000))))
Out[18]: