最终SQL考试的练习题。 列出收集总额(罚款和费用)以及犯罪分类所拥有的总金额。
这是我的代码。请告诉我它为什么不起作用。
select classification, fine_amount+court_fee as "collections amount", fine_amount - amount_paid as "Amount owned"
from crime_charges join crimes using (crime_id)
group by classification;
抛出错误:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 1 Column: 23
答案 0 :(得分:2)
试试这个:
SELECT classification, SUM(fine_amount+court_fee) AS "collections amount",
SUM(fine_amount - amount_paid) AS "Amount owned"
FROM crime_charges
INNER JOIN crimes USING (crime_id)
GROUP BY classification;
答案 1 :(得分:0)
您必须将select子句中的所有列放入group by子句中。
答案 2 :(得分:0)
尝试此查询:
select classification, fine_amount+court_fee as "collections amount", fine_amount - amount_paid as "Amount owned"
from crime_charges join crimes using (crime_id)
group by classification,fine_amount,court_fee,amount_paid;
答案 3 :(得分:0)
@shabeer,感谢您对学习的兴趣。但是,将理论学习投入时间总是明智的。你的问题非常重要。
使用group by
的选择查询只能包含聚合函数和select子句中的列。
试想一下,当您已经使用fine_amount
列对dataa进行分组时,数据库如何知道选择classification
列的单个值。它可能是max
,min
,sum
等等。
不是我的建议,但我只是觉得,当你开始了解基础知识时,你会感受到不同。当您在互联网上搜索解决方案时,它可能会成为一种习惯!
一切顺利!