Home > database >  How to get the latest value of a time duration (let's say for 1 minute) in Kusto table
How to get the latest value of a time duration (let's say for 1 minute) in Kusto table

Time:07-22

I want to find the latest value of a column for particular time duration(1 minute in my case) from Kusto table. I have timeseries data in PostgreSQL table and I am using last() function (https://docs.timescale.com/api/latest/hyperfunctions/last/)to find the latest value of scaled_value for 1 minute time bucket of PostgreSQL and I want to use same function in Kusto Table to get the latest value of scaled_value . What will be correct function to use in Kusto corresponding to last() function in Postgresql

Code I am using in PostgreSQL :

SELECT CAST(EXTRACT(EPOCH FROM time_bucket('1 minutes', timestamp) AT TIME ZONE 'UTC') * 1000 AS BIGINT) as timestamp_epoch,
vessel_telemetry.timeSeries, 
last(vessel_telemetry.scaled_value, vessel_telemetry.timestamp) as scaled_value, 
FROM shipping.vessel_telemetry 
WHERE vessel_telemetry.ingested_timestamp >= '2022-07-20T10:10:58.71Z' AND vessel_telemetry.ingested_timestamp < '2022-07-20T10:15:33.703985Z' 
GROUP BY time_bucket('1 minutes', vessel_telemetry.timestamp), vessel_telemetry.vessel_timeSeries

Corresponding code I am using in ADX

VesselTelemetry_DS 
| where ingested_timestamp >= datetime(2022-07-20T10:10:58.71Z) and ingested_timestamp < datetime(2022-07-20T10:15:33.703985Z) 
| summarize max_scaled_value = max(scaled_value) by bin(timestamp, 1m), timeSeries 
| project timestamp_epoch =(datetime_diff('second', timestamp, datetime(1970-01-01)))*1000, timeSeries, max_scaled_value

The data that i am getting using PostgreSQL is not matching with the data I am getting from ADX Query. I think the functionality of last() function of Postgre is different from max() function of ADX. Is there any function in ADX that we can use to perform same as last() of PSQL

CodePudding user response:

arg_max()

arg_max (ExprToMaximize, * | ExprToReturn [, ...])

Please note the order of the parameters, which is opposite to Timescale's last() -
First the expression to maximize, in your case timestamp and then the expression(s) to return, in your case scaled_value

  • Related