Relix

Problem solving

Every: related to all of a set, or all rows pass a test

Grain: one row per entity · Class: Universal · Signals: every, all, always, only, never failed · Operators: ÷, ∀, ▷

The problem

"Which suppliers hold every certification we require, and have delivered on time every time?"

How to recognise it

The question says every, all, always, only or never failed. It is about a set of things an entity has, or a history an entity has built up, and it keeps the entity only when the whole set passes.

The word every covers three different questions, and telling them apart is most of the work:

The questionWhat it asksReach for
"holds every required certificate"the entity's set contains a given set÷
"delivered on time every time"each of the entity's rows passes a test∀
"holds only approved certificates"nothing in the entity's set is outside a given set− and ▷

Exactly these is the first and the third together.

The data

A procurement team keeps the certifications each supplier holds, the ones it requires, and a delivery log. Delta holds no certificates and has never delivered.

Certifications := [
| supplier | cert    |
|----------|---------|
| Acme     | ISO9001 |
| Acme     | HACCP   |
| Birch    | ISO9001 |
| Cobalt   | ISO9001 |
| Cobalt   | HACCP   |
| Cobalt   | Organic |
];

Required := [
| cert    |
|---------|
| ISO9001 |
| HACCP   |
];

Suppliers := [
| supplier |
|----------|
| Acme     |
| Birch    |
| Cobalt   |
| Delta    |
];

Deliveries := [
| supplier | delivery | status  |
|----------|----------|---------|
| Acme     | 1        | on time |
| Acme     | 2        | on time |
| Birch    | 3        | on time |
| Birch    | 4        | late    |
| Cobalt   | 5        | on time |
];

Recipe 1: contains a set (÷)

Divide the pairs by the set that must be covered. The result keeps the columns of the left relation that are not in the right, so here one row per supplier.

Query
FullyCertified := { Certifications ÷ Required };
query { FullyCertified };
Result
 supplier
 ────────
 Acme
 Cobalt
(2 rows)

Cobalt's extra Organic does not matter: division asks whether the set is covered, not whether it matches.

Recipe 2: every row passes (∀)

Group by the entity and keep the group only when every row satisfies the condition.

Query
AlwaysOnTime := { ∀ supplier : status = "on time" (Deliveries) };
query { AlwaysOnTime };
Result
 supplier
 ────────
 Acme
 Cobalt
(2 rows)

Recipe 3: only from a set (− and ▷)

"Only" is the negation turned round: an entity qualifies when no row falls outside the set. Find the rows that fall outside with an anti-join, then take their owners away from everyone.

Query
Unapproved := { Certifications ▷ Certifications.cert = Required.cert Required };
OnlyApproved := { π supplier (Certifications) − π supplier (Unapproved) };
query { OnlyApproved };
Result
 supplier
 ────────
 Acme
 Birch
(2 rows)

Exactly the required set is both at once:

Query
ExactlyRequired := { FullyCertified ∩ OnlyApproved };
query { ExactlyRequired };
Result
 supplier
 ────────
 Acme
(1 row)

Putting it together

The question at the top had two *every*s, so it is two views and an intersection:

Query
Approved := { FullyCertified ∩ AlwaysOnTime };
query { Approved };
Result
 supplier
 ────────
 Acme
 Cobalt
(2 rows)

Pitfalls

Check it