question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for first(returnField,searchField) and last(returnField,searchField) function

See original GitHub issue

Use 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
proddatacommented, Sep 17, 2022

CTE + JOIN

WITH cte AS (SELECT device_id, MAX(ts) ts FROM data GROUP BY 1)
SELECT r.*
FROM data r JOIN cte using(device_id,ts)
WHERE r.time >= (SELECT min(ts) FROM cte);

CTE/JOIN: 12 seconds on the same set 🚀 caveat: only works fast, if all devices report values regularly .

alternative

WITH cte AS (SELECT device_id, MAX(ts) ts FROM data GROUP BY 1)
SELECT r.*
FROM data r JOIN cte using(device_id,ts)
WHERE r.time IN (SELECT ts FROM cte)

CTE + concatenated predicates

WITH cte AS (SELECT device_id, MAX(ts) ts FROM data group by 1)
SELECT r.*
FROM data r
where
  r.ts in (SELECT ts FROM cte)
  AND
  CONCAT(ts,',',device_id) in (SELECT CONCAT(ts,',',device_id) FROM cte);
-- 6.189 seconds 🚀

CTE + object comparison

WITH CTE AS (SELECT device_id, MAX(ts) ts FROM data group by 1)
SELECT r.*
FROM data r
where
  r.ts in (SELECT ts FROM cte) AND
  {ts=ts, device_id=device_id} in (SELECT {ts=ts, device_id=device_id} FROM cte);
-- 8.818 seconds
1reaction
hlcianfagnacommented, Sep 16, 2022

There is another faster workaround

SELECT
    device,
    split_part(
        MAX(
            CONCAT (date_format(ts), ',', speed::STRING)
        )
        /* optionally FILTER(WHERE speed  IS NOT NULL)
         to mimic IGNORE NULLS OVER ... */
        ',',
        2)::DOUBLE AS last_speed_in_groupbybucket_over_orderby_ts
FROM
    data
GROUP BY
    device;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found