Support for first(returnField,searchField) and last(returnField,searchField) function
See original GitHub issueUse case: In timeseries use cases one might often want to get the latest or first value for a specific device/machine/thing. This can be achieved with sub-queries, which might become rather cumbersome and/or slow.
Therefore it would be convenient to have a (fast) function that returns the value of another field/column when searching for the max/min value of another column.
Feature description:
first(returnField, searchField)
last(returnField, searchField)
first()
returns the value of column returnField
where searchField
is the minimum.
last()
returns the value of column returnField
where searchField
is the maximum.
e.g. get the latest reported speed for every device:
SELECT device, last(speed, ts)
FROM data
GROUP BY device;
(slow) workaround:
SELECT device, speed
FROM data a,
(
SELECT max(ts) AS ts, device
FROM data
GROUP BY device
) b
WHERE (a.ts = b.ts)
AND (a.device = b.device)
Some DBs call these max_by
and min_by
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
nagrestconf/check_multi at master - GitHub
directory to contain automatically created service definitions ... macro format: $type:return-field:search-field:search-value$.
Read more >/usr/lib/nagios/plugins/check_multi - APT Browse
This file is indexed. /usr/lib/nagios/plugins/check_multi is in nagios-plugin-check-multi 0.26-2. This file is owned by root:root ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
CTE + JOIN
CTE/JOIN: 12 seconds on the same set 🚀 caveat: only works fast, if all devices report values regularly .
alternative
CTE + concatenated predicates
CTE + object comparison
There is another faster workaround