0

Oracle 10g,sql 抛出异常:“not a group by expression”

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
        ) t where t.hbsag=1 and t.sgpt>=20

然后,我更改了 'group by' 参数,添加 'h.hbsag' 'h.sgpt',例如:

group by h.personal_info_id,h.hbsag,h.sgpt

但结果不正确。

4

3 回答 3

1

Thanks everyone,now I have already solved the problem.The query is:

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
于 2012-07-12T08:17:54.583 回答
0

其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

更新:

由于您只是使用count,需要什么来获取其他列?尝试这个..

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;
于 2012-07-11T07:52:02.113 回答
0

您应该添加不属于聚合函数的所有列。尝试这个。

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 
于 2012-07-11T07:49:01.307 回答