Skip to content
Advertisement

SQL query for entries of last N jobs

I have the following table:

etc, so basically jobs keep adding entries concurrently

I am looking for an efficient query to return all entries of the latest (by time) N jobs

So find the first (by time) entry for each job, then using that get the latest N jobs with all their entries

So for the above table and N=3 I will get:

I understand such query will involve PARTITION BY but not sure how to proceed ?

(I need a valid SqlLite query)

Advertisement

Answer

You need this query:

that returns the last 3 distinct JobIDs.

You can use it with the operator IN:

See the demo.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement