groupinggreatest-n-per-groupdolphindb# How to calculate the average of the last four records within each group in DolphinDB?

I want to calculate the average of the last four records in each group. I'm wondering if there's an efficient approach to achieve this. Could someone share any tips or methods?

Solution

If you are working with time-series data, you can utilize `aggrTopN`

to extract the last four records sorted by time. Refer to the following script:

```
sym = rand(`A`B`C, 15)
price= rand(49..51, 15)
qty = rand(100..200, 15)
time = 09:47:00 + 1..15
t1 = table(timestamp, sym, qty, time, price);
select aggrTopN(avg, price, time, 4, false) from t1 group by sym
```

- php Two-dimensional arrays are sorted and grouped according to the value of the key
- Remove unneeded level from pdo's fetchAll(PDO::FETCH_GROUP | PDO::FETCH_CLASS) data to form an associative array of objects
- Group Events by Week Commencing in Eloquent
- how group apiResource route and other routes together?
- Group laravel collection by 5 minutes
- Basic - T-Test -> Grouping Factor Must have Exactly 2 Levels
- Group rows in a collection by specified columns to build a 3d structure
- Grouping MYSQL query results base on sum of data in multiple column
- Group recurring values from a flat array into their own respective subarrays
- Split a flat array into a 2d array by starting a new subarray when an empty element is encountered
- R - how to assign night time GPS data (i.e data which crosses over midnight) the same start date?
- Pasting items in a vector and grouping them into multiples of x, separated by whitespace
- Grouping of dataframe to find the count and sum on columns
- Add disabled attribute to taxonomy's alphabetical pagination items when no terms exist in the group
- How can I group the items in a list according to a "key" in the list elements?
- using lodash .groupBy. how to add your own keys for grouped output?
- PostgreSQL crosstab query with multiple columns and week day
- Calculate sum of mapped values for each unique value in a flat array
- Calculate totals for each unique 2nd level data set in a 3d array
- Group and sum mapped values from two flat arrays of equal length
- Evenly distribute QDate values into certain number of slots
- Group List by property
- Group row data from a 2d array by one column and sum another column within each group
- Convert array of `YYYMMDD` date strings to a delimited string of days grouped by month
- Group multidimensional array data by two values and sum another value within each group
- Grouping a list of integers with nearest values
- How to select n values spaced as evenly as possible between the minimum and maximum in r?
- What is the most efficient way to implement GroupBy in Javascript?
- Slice find all structs where one property has the same value
- Creating groups with common relationships out of a dataframe