http://topic.csdn.net/u/20110815/13/dc3beb2e-ab51-4f0f-91c1-ba3e9d4772ff.html
问题还没有完全解决,当aid相同num也相同时,会重复计算,我想只取其中一条,然后求和

解决方案 »

  1.   

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

  2.   

    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)
      

  3.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  4.   

    数据库为mysql
    表为:
    create table ab(
     id int not null auto_increment,
     aid int,
     num int
    ) primary key(id);
    表记录为:
    insert into ab values(1, 1, 10)
    insert into ab values(2, 2, 12)
    insert into ab values(3, 3, 1)
    insert into ab values(4, 1, 12)
    insert into ab values(5, 2, 13)
    insert into ab values(6, 3, 14)
    insert into ab values(7, 4, 20)
    insert into ab values(8, 1, 1)
    insert into ab values(9, 2, 2)
    insert into ab values(10, 3, 18)
    insert into ab values(11, 5, 12)
    insert into ab values(12, 2, 13)我想得到不同aid的最大值之和,用HQL实现
    (4, 1, 12)
    (12, 2, 13)
    (10, 3, 18)
    (7, 4, 20)
    (11, 5, 12)
    12+13+18+20+12=75
    已有的方法:
    1、Select sum(num) From (
    select aid,max(num) as num from ab group by aid)T
    可以得到正确答案,但HQL不允许嵌套查询
    2、select sum(num) from ab t where not exists (select 1 from ab where aid=t.aid and num>t.num)
    会取到两个aid为2的记录,因为都为13,结果为88,不是我想要的
    3、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)
    测试了一下,查询无结果
    望各位高手支招,谢谢
      

  5.   

    根据你的需求,我搭建的测试环境:
    CREATE TABLE ab(
     id INT ,
     aid INT,
     num INT
    ) ;INSERT INTO ab VALUES(1, 1, 10);
    INSERT INTO ab VALUES(2, 2, 12);
    INSERT INTO ab VALUES(3, 3, 1);
    INSERT INTO ab VALUES(4, 1, 12);
    INSERT INTO ab VALUES(5, 2, 13);
    INSERT INTO ab VALUES(6, 3, 14);
    INSERT INTO ab VALUES(7, 4, 20);
    INSERT INTO ab VALUES(8, 1, 1);
    INSERT INTO ab VALUES(9, 2, 2);
    INSERT INTO ab VALUES(10, 3, 18) ;
    INSERT INTO ab VALUES(11, 5, 12);
    INSERT INTO ab VALUES(12, 2, 13);SELECT SUM(max_num) FROM (
    SELECT aid,MAX(num) AS max_num FROM ab GROUP BY aid
    ) a ;结果是:75
      

  6.   

    CREATE TABLE ab(
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      aid INT,
      num INT
    ) ;INSERT INTO ab VALUES(1, 1, 10);
    INSERT INTO ab VALUES(2, 2, 12);
    INSERT INTO ab VALUES(3, 3, 1);
    INSERT INTO ab VALUES(4, 1, 12);
    INSERT INTO ab VALUES(5, 2, 13);
    INSERT INTO ab VALUES(6, 3, 14);
    INSERT INTO ab VALUES(7, 4, 20);
    INSERT INTO ab VALUES(8, 1, 1);
    INSERT INTO ab VALUES(9, 2, 2);
    INSERT INTO ab VALUES(10, 3, 18);
    INSERT INTO ab VALUES(11, 5, 12);
    INSERT INTO ab VALUES(12, 2, 13);SELECT SUM(NUM) FROM AB A WHERE NOT EXISTS(SELECT 1 FROM AB WHERE (A.AID=AID AND A.NUM<NUM)
    OR (A.`aid`=AID AND A.`num`=NUM AND A.`id`<ID)
    )
      

  7.   

    HQL 建议到JAVA版去咨询一下。