有这样一个数据库表:
create table ab(
 id int not null auto_increment,
 aid int,
 num int
) primary key(id);
表记录为:
'1', '1', '10'
'2', '2', '12'
'3', '3', '1'
'4', '1', '12'
'5', '2', '13'
'6', '3', '14'
'7', '4', '20'
'8', '1', '1'
'9', '2', '2'
'10', '3', '18'
'11', '5', '12'
现在需要统计不同aid的最大值,并求和
要求用hql实现
我是这样写的:select sum(max(num)) from `test`.`ab` group by(aid);
但报非法group funcation错误不知道大家有什么好办法?

解决方案 »

  1.   

    select sum() from ( select max(num) as  num from ab group by aid) t;
      

  2.   

    关键是需要用HQL编写,而且HQL不支持嵌套,只支持where子句查询
      

  3.   

    select sum(num) from ab t where not exists (select 1 from ab where aid=t.aid and num>t.num);
      

  4.   


    没用过 hql,sql 语法里 group by 不用括号的,带括号了认为是一个函数。hql 是有括号的?
      

  5.   

    select id,sum(num) as sum,max(num) as max from test group by aid
      

  6.   


    如何想去掉相同aid记录(num 值一样),该怎么办?
      

  7.   

    Select sum(num) From (
    select aid,max(num) as num from ab group by aid)T
      

  8.   

    select sum(num) from ab t where not exists (select 1 from ab where aid=t.aid and num>t.num)
    and not exists (select 1 from ab where aid=t.aid and num=t.num)