选择更多列以及两个带分组的计数

时间:2017-04-25 14:53:52

标签: sql oracle

我正在尝试提出一个解决方案,列出几乎所有表的列以及按两个不同字段分组的两列的计数,我可以使用它创建两个仪表板,其中包含按实体和类型过滤的计数。 / p>

如下所示。

select c.Entity,C.Type,C.TestID,C.a,C.b,C.x,C.y,C.z,
       count (TESTID) as CountbyEntity,
       count (TESTID) as CountbyType
from Testplan c group by Entity, Type

我想将以下两个查询包含在其余列中,而不必使用单独的SQL。

select count (TESTID) as CountbyType,Type 
from TESTPLAN 
where Type is not null 
group by Type

select count (TESTID) as CountbyEntity,Entity 
from TESTPLAN 
where Entity is not null 
group by Entity

1 个答案:

答案 0 :(得分:0)

除非我遗漏了某些内容,否则你应该使用count() over(partition by)代替group by

select c.Entity,C.Type,C.TestID,C.a,C.b,C.x,C.y,C.z,
       count (TESTID) over(partition by Entity) as CountbyEntity,
       count (TESTID) over(partition by Type) as CountbyType
from Testplan c 

有关详情,请参阅Analytic functions by Example博文。