extract highest row from array

Team,

I have an array of results from query and want to use the rows which have latest timestamp.

So array have data like (username :abc,id,1,timestamp1) , same user have another entry but with different timestamp. Max is not doing the trick , any idea ?

  Discussion posts and replies are publicly visible

  • Does the Dataset comes from the DB?

    If yes, you can use a!paginginfo and with the a!sort get the data in chronological order.

  • Hello Manuel,

    Thank you . Yes , using sort info i am able to get the top rows sorted in order but as said there are many rows in returned dataset and i want the latest top rows out to show in grid as unique data else multiple users are appearing in grid , i want unique usernames with latest timestamp.

  • You can do this using an aggregation. If you group by the username and return the MAX of the date, then it should return the most recent item. Here's an example:

    a!queryEntity(
      entity: cons!YOUR_DSE,
      query: a!query(
        aggregation: a!queryAggregation(
          aggregationColumns: {
            a!queryAggregationColumn(
              field: "username",
              isGrouping: true
            ),
            a!queryAggregationColumn(
              field: "dateUpdated",
              alias: "dateUpdated_max",
              aggregationfunction: "MAX",
            )
          }
        ),
        pagingInfo: a!pagingInfo(
          startIndex: 1,
          batchSize: 50,
          sort: a!sortInfo(
            field: "dateUpdated_max",
            ascending: false
          )
        )
      ),
      fetchTotalCount: false
    )

  • Thank Peter. I have tried below code but it says MAX cannot be applied to timestamp (Expression evaluation error at function a!queryEntity [line 2]: Cannot apply aggregation function [MAX] to field [timestamp]. The valid data types for [MAX] are [[Number (Integer), Number (Decimal), Interval (Day to Second), Boolean, Date, Date with Timezone, Date and Time, Date and Time with Timezone, Time]].)

    If i change MAX with count it says (Expression evaluation error at function a!queryEntity [line 2]: Cannot sort by [timestamp]. When grouping, provide the alias in order to sort by one of the grouped or aggregated fields.)

    Snippet of code:

    In output i want (username,firstname,timestamp(latest),created) from view.

    with(
    a!queryEntity(
    entity: cons!VW,
    query: a!query(
    aggregation: a!queryAggregation(
    aggregationColumns:
    {
    a!queryAggregationColumn(
    field: "username",
    isGrouping: true
    ),
    a!queryAggregationColumn(
    field: "timestamp",
    alias:"last_login",
    aggregationFunction: "COUNT"
    )
    }
    ),
    logicalExpression: a!queryLogicalExpression(
    operator: "AND",
    filters: {
    a!queryFilter(
    field: "username",
    operator: "in",
    value: ri!user
    ),
    a!queryFilter(
    field: "timestamp",
    operator: ">=",
    value: ri!year & "-01-01 00:00:00"
    ),
    a!queryFilter(
    field: "timestamp",
    operator: "<=",
    value: ri!year & "-03-31 00:00:00"
    )
    }
    ),
    pagingInfo: a!pagingInfo(1,-1,a!sortInfo("timestamp",false))
    )
    )
    )

  • seems timestamp is getting stored in text format in DB and CDT is also having text

  • Yeah this will only work if it's actually a timestamp - there isn't a concept of a "MAX" for a text field, so it wouldn't be supported. Can you update the column in the database to use a timezone instead? I also don't think your filters are going to work if it's actually stored in the database as a text type.