我有一个非常简单的查询(在Oracle 11g上)来选择3个字段:
select field1, field2, field3, count(*) from table
where...
group by field1, field2, field3
having count(*) > 10;
现在,我需要的是从“group by”中排除“field3”,因为我只需要将字段1和2分组,但我还需要输出中的field3。 据我所知,选择中的所有字段也必须在“分组依据”中报告,那么我该如何处理呢?
由于 卢卡斯
答案 0 :(得分:3)
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
答案 1 :(得分:2)
使用“计数”功能的analytical version:
select * from (
select field1, field2, field3, count(*) over(partition by field1, field2) mycounter
from table )
--simulate the having clause
where mycounter > 10;
答案 2 :(得分:1)
如果您不再按field3
分组,则每组可能会突然出现field3
个不同的情况。您必须决定要显示哪一个,例如最大值:
select field1, field2, max(field3), count(*) from table
where...
group by field1, field2
having count(*) > 10;