Tech Tip: Comparing two fields in a Dataclass query
PRODUCT: 4D | VERSION: 17 R6 | PLATFORM: Mac & Win
Published On: August 14, 2019
4D v17R6 has improved the query function on dataclasses. Prior to v17R6, if a comparison of two fields were performed it might not return the expected results as the documentation states that the query string's format is
attributePath comparitor value
and value is expected to be a static value and not another attribute path as the value will not update itself for each entity/record.
4D v17R6 improves the query function by allowing formulas to be passed which can be evaluated passing it in as an eval() statement. The eval() statement can evaluate This, allowing each entity/record to be parsed and compared based on the formula.
Take the following Table:
A quick attempt at comparing two fields of the table would look something like the following:
$res1:=ds.Table.query("Num_1 > Num_2") |
As shown in the results, it appears that all entities were returned including entities where Num_1 = Num_2 and Num_1 < Num_2 (most likely due to Num_2 in the expression being evaluated to a value of 0).
Utilizing the a formula the following would be the proper way to compare the two fields:
$res2:=ds.Table.query("eval(This.Num_1 > This.Num_2)") |
With the formula evaluated for each record, a correct entity selection is generated where Num_1 > Num_2.