« Return to TrackVia.com
TrackVia

Child Lookups

Child Lookups

In many applications with relational data, it's useful to aggregate data from child records into parent records. In other words, you might want to include in the parent record a count of child records, or the sum of a certain field across all children. TrackVia's child lookup functions allow you to do this.

Note: This video demo accompanied by these tips & examples provide an in-depth review of child lookups.

Example

Suppose you have an order tracking system that consists of two tables. The parent table is called Orders, and contains general information about the order: customer name, ship-to address, etc. The child table is called Order Line Items, and contains the individual components of the order.

Each line item has a product number, quantity, price, etc. In parent order records, you'd like an automatically-calculated number that tells you how many line items the order has. You'd also like a calculated total order amount, which is the sum of all the Price fields across the child records for any given order.

Counting child records

To calculate the number of child records, use the childcount function. First, create a calculated number field in your parent table. Give it a name like Number of line items. The formula would be:

childcount("Order Line Items", "Order Number")
 

Here, "Order Line Items" is the name of the child table you want to count records in. "Order Number" is the name of the field in that child table which links back to the parent table. (You have to provide these two inputs because TrackVia allows multiple child tables to link to a single parent table, and multiple distinct links from a child table to the same parent table.) Note that the field names are in quotation marks, which is different than the way fields are normally referenced in a formula.

When you add or remove child records for a single parent, this formula will automatically update with the count of children (though you might have to refresh your browser to see the latest number in a view or a parent record detail screen).

Summing child values

To calculate the sum of the Price field across child records, use the childsum function. First, create a calculated currency field in your parent table. Give it a name like Order Total. The formula would be:

childsum("Order Line Items", "Order Number", "Total Cost")
 

As before, "Order Line Items" is the name of the child table you want to sum records from, and "Order Number" is the name of the field in that child table that links back to the parent table. The third input, "Total Cost", is the name of the field that you want summed across all children. This Order Total field will now be automatically updated whenever a child record is added, deleted, or changed.

Getting fancy

Child lookup functions can be combined with parent lookup functions and other formulas to create powerful applications. As an example of how these might be used together, suppose the Order Line Items table in the above example included a link-to-parent field that linked to a Products table. The price of the product would be pulled into each line item record using a calculated currency field, with a formula of parentlookup("Product","Price") (see the Parent Lookups article). Then a calculated field called Total Cost would create a subtotal for each line item, with a formula of Quantity * Price. Finally, this Total Cost (not the product's price field) is what would be summed into the parent Order record.

Child lookup functions can be used with numeric fields (numbers, currencies, or percentages), date fields, text fields, drop-down or checkbox fields, or any other TrackVia field type. Furthermore, in addition to counting and summing child records, these functions can find minimum or maximum values among children, and combine text values. Here are the other child lookup functions available:

Childmin

This returns the smallest value of a field among all child records. For example, to find the earliest ship date among the line items in our order, we'd use

childmin("Order Line Items", "Order Number", "Ship date") 
 
This returns the largest value of a field among all child records. For example, to find the latest ship date among the line items in our order, we'd use
 
childmax("Order Line Items", "Order Number", "Ship date") 
 
This returns a count of distinct values (ignoring blanks) among a certain field in the child records. For example, to find the number of distinct product IDs across all line items in the order, we'd use
 
childcountdistinct("Order Line Items", "Order Number", "Product ID")
 
This returns a count of non-blank values among a certain field in the child records. For example, to find the number of line item records that have the "Fragile" checkbox checked, use
 
             childcountnonblank("Order Line Items", "Order Number", "Fragile")
 
This combines values from children into a single text field in the parent. Use this function in a calculated text field. For example, in a view of all orders in the parent Orders table, you might want to see a compact list of the product IDs included in each order. The formula for this would be
 
childconcatenate("Order Line Items", "Order Number", "Product ID")
 
The result will be a list of the Product ID values from the children, separated by commas (something like AB-2982, AA-2400, AB-1920, BQ-1905). These are the same values that would be counted by countnonblank().
 
This is the same as childconcatenate, but only includes distinct values:
 
childconcatenatedistinct("Order Line Items", "Order Number", "Product ID")
 
The result is a list of values, the same values that would be counted by countdistinct().
In either childconcatenate() or childconcatenatedistinct(), you can provide an optional separator that TrackVia will use in the list instead of a comma and a space. See the technical details in the Lookup Functions article.