Skip to content
Advertisement

SQL query 2 limits

I have the following issue i want to limit on the total amount of items returned while limiting one value to a certain number

I do a query like this

Now I want to do the following, I have a field called running and I want to be able to say that in these results I want to have maximum n results that are in not running, e.g. 2 but I still want to get 5 results back based on the priority

So if I have for example the following data in the DB:

And I have a total limit of 5 and a non-running limit of 2 I expect the following result

Does someone have a suggestion how to do it in PostgreSQL?

Update: I got the query that gives the result that I want based on @Gordon Linoff answer

Link http://sqlfiddle.com/#!17/98477/1

Advertisement

Answer

I think you want:

Here 3 = 5 – 2.

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