SQL query to get lowest 2 values of a counted query selection (using db2)?

Posted by jNoob on Stack Overflow See other posts from Stack Overflow or by jNoob
Published on 2010-06-11T15:08:56Z Indexed on 2010/06/11 15:13 UTC
Read the original article Hit count: 119

Filed under:
|
|
|
|

Hi,

Imagine I already have a query that returns the following:

Col1 | Col2
------------
 A   |   2
 B   |   3
 C   |   3
 D   |   4
 E   |   8
    ...

Say I used something like this:

select Col1, count ( * ) as Col2 \ 
from ...
where ...
order by Col2 \ 
group by Col1 \ 

So now, all I want to select are (Col1, Col2) such that it returns the selections (a, b) and (c, d) where (b >= all (Col2)) and (d >= ((all (Col2)) - a)). So for the above example, it would return {(A, 2), (B, 3), (C, 3)}. How do I go about doing this?

Any help would be highly appreciated. Thanks.

© Stack Overflow or respective owner

Related posts about sql

Related posts about homework