for the table below
tenant_id | status_id
----------------------
3 | 6
---------------------
5 | 7
---------------------
7 | 7
--------------------
3 | 7
-------------------
3 | 7
--------------------
5 | 7
we can see that:
- tenant_id=3 has 1 record with status_id=6
- tenant_id=5 has 2 record with status_id=7
- tenant_id=7 has 1 record with status_id=7
- tenant_id=3 has 2 record with status_id=7
For given STATUS_ID_PARAM I want to get all tenants that has the most records with that STATUS_ID_PARAM.
For the example above, for STATUS_ID_PARAM = 7, the query should return 2 records:
tenant_id | status_id
--------------------
3 | 7
--------------------
5 | 7
because those tenants has the most records(2 for each one) with status_id=7.
I tried something like this, but I don’t know how to continue or may be there is another way:
select tenant_id, count(status_id) s
from candidate
where status_id = STATUS_ID_PARAM
group by tenant_id, status_id
This will work in both Oracle and SQL Server
(Tested on SQL Server 2005->2012 and Oracle 11g R2)