Java 关于oracle sql group by';不是按表达式分组';
Oracle 10g,sql抛出异常:“不是按表达式分组” 然后,我更改了“分组依据”参数,添加了“h.hbsag”“h.sgpt”,如:Java 关于oracle sql group by';不是按表达式分组';,java,sql,oracle10g,Java,Sql,Oracle10g,Oracle 10g,sql抛出异常:“不是按表达式分组” 然后,我更改了“分组依据”参数,添加了“h.hbsag”“h.sgpt”,如: group by h.personal_info_id,h.hbsag,h.sgpt 但是结果不正确。您应该添加不属于聚合函数的所有列。试试这个 select count(*) from ( select h.personal_info_id pid,h.hbsag hbsag,h.sgpt sgpt,MAX(h.date_reported)
group by h.personal_info_id,h.hbsag,h.sgpt
但是结果不正确。您应该添加不属于聚合函数的所有列。试试这个
select count(*) from
(
select h.personal_info_id pid,h.hbsag hbsag,h.sgpt sgpt,MAX(h.date_reported)
from health_checkup_info h inner join personal_info p on h.personal_info_id = p.id
where h.deleted = 0 and h.date_reported is not null and h.hbsag in(1,2) and p.deleted = 0
group by h.personal_info_id ,h.hbsag ,h.sgpt
) t
根据Oracle的行为,除聚合函数外的所有选定列都必须按分组
select count(*)
from (
select h.personal_info_id pid,h.hbsag hbsag,h.sgpt sgpt,MAX(h.date_reported)
from health_checkup_info h
inner join personal_info p on h.personal_info_id = p.id
where
h.deleted = 0
and h.date_reported is not null
and h.hbsag in(1,2)
and p.deleted = 0
group by h.personal_info_id,h.hbsag,h.sgpt
) t
更新:
由于您只是在使用计数
,需要什么来获取其他列??试试这个
select count(*)
from (
select h.personal_info_id pid,MAX(h.date_reported)
from health_checkup_info h
inner join personal_info p on h.personal_info_id = p.id
where
h.deleted = 0
and h.date_reported is not null
and h.hbsag in(1,2)
and p.deleted = 0
group by h.personal_info_id
) t;
谢谢大家,现在我已经解决了这个问题,问题是:
select count(*)
from (
select h.personal_info_id pid,h.hbsag,ROW_NUMBER() OVER (partition by h.personal_info_id order by h.date_reported desc) r
from health_checkup_info h
inner join personal_info p on h.personal_info_id = p.id
where
h.deleted = 0
and h.date_reported is not null
and h.hbsag in(1,2)
and p.deleted = 0
) t where t.hbsag=2 and r=1
您不需要在GROUPBY子句中指定列别名。我想这是复制/粘贴的东西:)@Madhivanan,谢谢你,伙计。你明白了:)修好了@曼努拉贾达,谢谢你!但如果使用“h.personal\u info\u id、h.hbsag、h.sgpt分组”,则结果不正确。例如,使用“按h.personal\u info\u id分组”,结果是2640。但是,使用“按h.personal\u info\u id分组,h.hbsag,h.sgpt”,结果是2641。为什么?是的,让我更新查询,这只是实现目标的另一个逻辑。@manurajhada不,不,不。我需要h.hbsag,h.sgpt,然后再次使用它。“按h.personal_info_id)t分组,其中h.hbsag=1和h.sgpt>=20”
select count(*)
from (
select h.personal_info_id pid,h.hbsag,ROW_NUMBER() OVER (partition by h.personal_info_id order by h.date_reported desc) r
from health_checkup_info h
inner join personal_info p on h.personal_info_id = p.id
where
h.deleted = 0
and h.date_reported is not null
and h.hbsag in(1,2)
and p.deleted = 0
) t where t.hbsag=2 and r=1