Search code examples
top-ntimescaledb

TimescaleDB Top N function


I have been reading through the docs but cannot seem to find something similar to Prometheus' topk function. Is there maybe a sort then limit?

As an example, lets say I wanted the top 10 hosts by cpu? Is that even possible?


Solution

  • You can use a combination of ORDER BY and LIMIT:

    SELECT   *
    FROM     hosts
    ORDER BY cpu DESC 
    LIMIT    10