cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Get last x entries from DataTable

Amethyst

Get last x entries from DataTable

Dear community,

 

I would like to retrieve the last x (e. g. 10) records (by id) from a DataTable. This post nearly covers my problem but Streams do not suite my data and the second solution does not work because I have an additional filter. Here is what I thought should work:

 

var query = {
    "sorts": [
        {
            "fieldName": "MessageId",
            "isAscending": false
        }
    ],
    "filters": {
        "type": "EQ",
        "fieldName": "Direction",
        "value": 2
    }
};

var entries = Things["MESMessageLogDT"].QueryDataTableEntries({
	maxItems: 10,
	query: query
});

MESMessageLogDT is the DataTable entity and I would like to get the last 10 records matching my filter criteria (Direction = 2), this is why I cannot simply take length - x.

 

However, the actual result is: The first 10 records but in descending order. So apparently the sequence of data processing is

  1. filter
  2. maxitems
  3. sort

while I need (and expected)

  1. filter
  2. sort
  3. maxitems

 

A workaround is this but the performance is poor and gets worse with every entry:

var entries = Things["MESMessageLogDT"].QueryDataTableEntries({
	maxItems: 9999999,
	query: query
});

entries = Resources["InfoTableFunctions"].TopN({maxItems:10, t:entries});

Is there any better approach?

 

Thanks

Benny

1 REPLY 1

Re: Get last x entries from DataTable

Stream entries come with oldest first y/n by default. DataTables unfortunately do not.

This is a design weakness in datatables for which at this time there is no way around.

at least I don't think there is a way around it.