Data

Tags

The tag operation has a variety of applications such as generating surrogate keys and finding the nth smallest or largest item.

Tag

The tag operator takes a relational expression, an ordered set of attributes and a tag attribute name. Each attribute name in the ordered set can be prefixed with - to use a descending order, otherwise an ascending order is used. An integer attribute is added to each tuple in the relational expression, incrementing within the common values of the ordered set.

Again, given the example SP relvar:

SP
SNOPNOQTY
S1P1300
S1P2200
S1P3400
S1P4200
S1P5100
S1P6100
S2P1300
S2P2400
S3P2200
S4P2200
S4P4300
S4P5400

We can issue tags within the SNO (ascending) using the tag operator.

For example:

tag(SP, {SNO}, p_serial)
SNOPNOQTYp_serial
S1P13000
S1P22001
S1P34002
S1P42003
S1P51004
S1P61005
S2P13000
S2P24001
S3P22000
S4P22000
S4P43001
S4P54002

Nth smallest

We can tag the results of a quota operation and then filter it to find the nth smallest item. For example, the part(s) with the 2nd smallest weight:

tag(quota(P{WEIGHT}, {WEIGHT}, 2), {-WEIGHT}, t)[t=0] & P
WEIGHTtPNOPNAMECOLORCITY
140P4ScrewRedLondon

Nth largest

We can tag the results of a quota operation and then filter it to find the nth largest item. For example, the part(s) with the 2nd largest weight:

tag(quota(P{WEIGHT}, {-WEIGHT}, 2), {WEIGHT}, t)[t=0] & P
WEIGHTtPNOPNAMECOLORCITY
170P2BoltGreenParis
170P3ScrewBlueRome

These could be made into relational operators - see the Relation operators section for details.