列出捐赠总额大于某事物的所有捐赠者

时间:2015-07-28 15:23:54

标签: sql oracle

这是表结构

捐赠者(捐献者ID ,姓名)

捐赠( ID ,捐赠ID,金额)

这是我的疑问:

SELECT donor.name, SUM(donation.amount) "Total"
FROM donor, donation
WHERE donor.donorID = donation.donorID
AND SUM(donation.amount) > 1000;
GROUP BY donor.name
ORDER BY SUM(donation.amount) DESC;

这是错误:

  

ORA-00934:此处不允许使用群组功能   00934. 00000 - “此处不允许使用群组功能”

当我删除总和(捐赠)时没有错误< 1000 如何整合它而不出错?请帮忙。

3 个答案:

答案 0 :(得分:7)

您不能在where子句中放置聚合函数。您需要having子句:

SELECT donor.name, SUM(donation.amount) "Total"
FROM donor, donation
WHERE donor.donorID = donation.donorID
GROUP BY donor.name
HAVING SUM(donation.amount) > 1000
ORDER BY SUM(donation.amount) DESC;

但是,您应该学会使用正确的join语法和表别名:

SELECT d.name, SUM(dn.amount) as "Total"
FROM donor d JOIN
     donation dn
     ON d.donorID = dn.donorID
GROUP BY d.name
HAVING SUM(dn.amount) > 1000
ORDER BY SUM(dn.amount) DESC;

答案 1 :(得分:1)

你需要像这样使用HAVING子句:

SELECT donor.name, SUM(donation.amount) "Total"
FROM donor, donation
WHERE donor.donorID = donation.donorID
GROUP BY donor.name
HAVING SUM(donation.amount) > 1000
ORDER BY SUM(donation.amount) DESC;

此外,正如Marc B指出的那样,分号位于终止查询的错误位置。

答案 2 :(得分:-1)

错字:

AND SUM(donation.amount) > 1000;
                               ^---terminates query
GROUP BY donor.name

group by不是开始新查询的有效方式。