Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORACLE Select and group by excluding one field

I have a very simple query (on Oracle 11g) to select 3 fields:

select field1, field2, field3, count(*) from table
where...
group by field1, field2, field3
having count(*) > 10;

Now, what I need, is exclude "field3" from the "group by" since I only need field 1 and 2 to be grouped, but I also need field3 in the output. As far I know, all the fields in the select must be reported also in "group by", so how can I handle that?

Thanks Lucas

like image 461
Lucas Rey Avatar asked Oct 22 '25 18:10

Lucas Rey


1 Answers

select t.field1, t.field2, t.field3, tc.Count
from table t
inner join (
    select field1, field2, count(*) as Count
    from table 
    where... 
    group by field1, field2 
    having count(*) > 10 
) tc on t.field1 = tc.field1 and t.field2 = tc.field2
like image 157
D'Arcy Rittich Avatar answered Oct 25 '25 09:10

D'Arcy Rittich