How to filter |
When you create a Retrieve template, you want to avoid getting more data than you need. The [Filter] section lets you restrict the data that TDE processes. The filter lists one or more Boolean expressions. When the template runs, TDE joins the expressions with the AND operator and uses the resulting expression to filter the data. Each row of the input data is evaluated against the filter, and TDE only processes rows that match every expression in the filter list.
Note |
---|
|
This topic contains the following sections:
When you work with Tables that have no required fetchers, you do not need a [Filter] section at all in your Retrieve template. However, if you want to get back a more useful subset of data, just add a filter or two that tests the value of one of the table's columns: TDE will return only those rows that pass every test.
Suppose that you want to find out how many of your markers are located in areas with higher speed limits, such as those over 40 mph. You can do this in a few steps:
SpeedLimit = GreaterThan(40)
[Template] TemplateVersion = 1.0 TableID = Marker-1.0 TemplateName = GetMarkerBySpeed Intent = Retrieve OrderBy = MarkerType,SpeedLimit [Output] label = Input.Tag speed = Input.SpeedLimit [Filter] SpeedLimit = GreaterThan(40)
Tip |
---|
Apply sorting to your results by adding one or more columns to an OrderBy = statement in your [Template] section. |
The FuelTransaction table requires a fetcher, which means that you need to use one of the five filter sets described in the table's Requirements.
Suppose that you want to fetch transactions by driver, which is the third fetcher option: PurchaseTime (Between) AND DriverName (Equals). You can set the filter to find static values, as long as you specify the datatype Timestamp for the strings you give for the dates:
[Template] TemplateVersion = 1.0 TableID = FuelTransaction-1.0 TemplateName = ReadByDriver Intent = Retrieve OrderBy = PurchaseTime [Filter] PurchaseTime = Between("2013-02-28", "2013-03-02") DriverName = Equals("Bruno Wesler") [Output] DriverName = Input.DriverName Account = Input.AccountNumber ...
Suppose that you want to fetch transactions by dates that are specified at run-time, so that you do not have to change the template each time. You can add a [User] section to define a variable for each run-time parameter. You create user variables with a few steps:
&Driver=Bruno%20Wesler&ReportStart=2013-08-01&ReportEnd=2013-09-01
[Template] TemplateVersion = 1.0 TableID = FuelTransaction-1.0 TemplateName = ReadByDriver Intent = Retrieve OrderBy = PurchaseTime [User] Driver(Text) = "" ReportStart(Timestamp) = "" ReportEnd(Timestamp) = "" [Filter] PurchaseTime = Between(ReportStart,ReportEnd) DriverName = Equals(Driver) [Output] DriverName = Input.DriverName Account = Input.AccountNumber ...
Suppose that you want this template to always fetch a week's worth of fuel transactions at a time. You can add a [Calculated] section and use .NET functions for DateTime to work with Timestamp data. You create calculated variables with a few steps:
&Driver=Bruno%20Wesler&ReportStart=2013-08-01
[Template] TemplateVersion = 1.0 TableID = FuelTransaction-1.0 TemplateName = GetFuelTransactionsForWeekByDriver Intent = Retrieve OrderBy = PurchaseTime [User] Driver(Text) = "" ReportStart(Timestamp) = "" [Calculated] ReportEnd = ReportStart.AddDays(7) [Filter] PurchaseTime = Between(ReportStart,ReportEnd) DriverName = Equals(Driver) [Output] DriverName = Input.DriverName Account = Input.AccountNumber ...
The following example demonstrates how to filter by Id. For additional information, see How to use keys for explanation of how to use Ids (external keys) with TDE templates.
[Template] TemplateVersion = 1.0 TableID = Signature-1.0 TemplateName = RetrieveSignature Intent = Retrieve [Filter] Id(SigId) = Equals("10031") [Output] Id(SigId) = Input.Id DriverId(DriverId) = Input.DriverId ...
As before, you can add a [User] section to transform the template into one that takes the Id as a run-time parameter:
&SId=10031
[Template] TemplateVersion = 1.0 TableID = Signature-1.0 TemplateName = RetrieveSignature Intent = Retrieve [User] SId = "" [Filter] Id(SigId) = Equals(SId) [Output] Id(SigId) = Input.Id DriverId(DriverId) = Input.DriverId ...
What if you need a template that can find more than one driver? You can use the filter condition In([x,y,z]) to specify the set of Ids against which TDE will find matches.
Using this condition lets you add variables to your [User] section to transform the template into one that takes multiple Ids as run-time parameters:
&ExtId=Abner&ExtId1=Riecher&ExtId2=Bosh&ExtId3=Elda
[Template] TemplateVersion = 1.0 TableID = Driver-1.0 TemplateName = RetrieveDriverByIds Intent = Retrieve OrderBy = Input.Nickname [User] ExtId(Text) = "" ExtId1(Text) = "" ExtId2(Text) = "" ExtId3(Text) = "" [Filter] Id(AcmeDriver) = In([ExtId,ExtId1,ExtId2,ExtId3]) [Output] Nickname = Input.Nickname EmployeeNo = Input.EmployeeNo ...
For a more efficient filter syntax, you can use a Set or Array data type to simplify the run-time parameter input:
[Template] TemplateVersion = 1.0 TableID = Driver-1.0 TemplateName = RetrieveDriverByIds Intent = Retrieve OrderBy = Input.Nickname [User] ExternalKeySet(Set<text>)="" # Set via query string: &ExternalKeySet=Moe,Larry,Curly,& ... [Filter] Id(ExtKey) = In(ExernalKeySet) [Output] Nickname = Input.Nickname EmployeeNo = Input.EmployeeNo ...