Skip to content
Advertisement

Select everything except rows with maxvalue in certain column

I have a table with data and I’m trying to select some values with lower value than maximum. I am using IBExpert, I need to do this in SQL because I am working on a Firebird database. My table looks like this:

What I want is to select only rows with GRUPA values lower than maximum, here max value is 3, but sometimes it might be 5, 7, 11 or whatever, so I can’t specify the value.

It should look like this:

I’ve tried with:

eliminating the max value, but result of that is simply nothing. When I use < instead of = before MAXVALUE(c2.GRUPA) I get:

obviously, as I am eliminating all values lower than max (as far as I understand that).

I also tried something much simpler:

but again I get nothing in results window. If I change “MAXVALUE(GRUPA)” to just “3” I get expected results, but again, I can’t specify the max value.

What am I doing wrong?

Advertisement

Answer

As far as I can tell from your question, you want:

That is: select all rows where GRUPA is not equal to the maximum value of GRUPA (for that ID_RODZICA).

As to why your queries don’t work:

In your first query, the subquery in the NOT EXISTS will always produce rows given the example data, and in a NOT EXISTS this means the result is always false, and therefor the query as whole produces no rows.

The second query you tried doesn’t work, because MAXVALUE does not do what you think it does. It is not an aggregate function, but instead from the list of values passed as parameters, it will return the highest value. Given you pass a single value, it will return that value. Which results in GRUPA < MAXVALUE(GRUPA) being equivalent to GRUPA < GRUPA which is never true, so again it produces no rows.

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