sql - 选择一个计数和一个大于一的最大值的计数
问题描述
我有一个查询需要计算一些列的一些计数和一列的最大值的计数,然后按更多标准分组。
到目前为止,我有以下查询:
select
subj.inventoryNum as Inventory,
extract(month from subj.createDate) as month,
oolame.schoolCode as Code,
count(case when max(subVers.verNum) > 0 then 1 end) as deleted,
count(case when subVers.delDate is not null then 1 end) as changed
from
Subjects subj
inner join SubjectVersions subVers on subVers.subjFk = subj.subjId
inner join SchoolName oolame on oolame.oolameId = subj.oolameFk
group by
subj.inventoryNum,
extract(month from subj.createDate),
oolame.schoolCode;
它给了我以下错误:
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
Error at Line: 2 Column: 1
解决方案
你想做什么有点不清楚。你想要整体最大值吗?每个科目的最大值?其他一些最大值?
在任何情况下,您都可以在子查询中使用窗口函数来获取最大值。例如,如果您想要每个主题的最大值:
select subj.inventoryNum as Inventory,
extract(month from subj.createDate) as month,
oolame.schoolCode as Code,
sum(case when subVers.max_verNum > 0 then 1 else 0 end) as deleted,
count(subVers.delDate) as changed
from Subjects subj inner join
(select subvers.*,
max(subVers.verNum) over (partition by subVers.subjFk) as max_verNum
from SubjectVersions subVers
) subVers
on subVers.subjFk = subj.subjId inner join
SchoolName oolame
on oolame.oolameId = subj.oolameFk
group by subj.inventoryNum,
extract(month from subj.createDate),
oolame.schoolCode;
推荐阅读
- javascript - 完整日历 - 检查同一天是否有活动
- office-js - 摆脱工作簿中的无效绑定
- typescript - 如何区分值和回调参数类型
- .net - .Net 核心 + React + Docker 中的 PostgeSQL
- angularjs - 路由重定向后页面上的 angulajs 数据未加载
- devexpress - 使用 devexpress 在一个项目中添加多个 UI
- javascript - 查询 OWL 本体
- javascript - JavaScript 箭头函数抛出未定义
- kubernetes - 如何在 kubernetes 上部署自定义 nginx 应用程序?
- node.js - 带有 requestSpotInstances 的 TagSpecifications UnexpectedParameter 带有 aws-sdk