3

我有一个非常简单的查询(在 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。据我所知,选择中的所有字段也必须在“分组依据”中报告,那么我该如何处理呢?

谢谢卢卡斯

4

3 回答 3

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
于 2012-08-16T21:08:58.810 回答
2

使用“count”函数的解析版本:

select * from (
select field1, field2, field3, count(*) over(partition by field1, field2) mycounter
from table ) 
--simulate the having clause
where mycounter > 10;
于 2012-08-19T15:53:41.020 回答
2

如果您不再分组,则每个组field3可能会突然不同。field3您必须决定显示哪一个,例如最大值:

select field1, field2, max(field3), count(*) from table
where...
group by field1, field2
having count(*) > 10;
于 2016-09-19T19:59:55.750 回答