![](/img/trans.png)
[英]how to sum a column from one table and group the sum using data from column of another table
[英]Query to sum amounts from one table and group it by a column in another table
如图所示,我有两个表, crime_charges
和Crime
crimes
。 我需要显示所欠罚款总额( fine_amount + court_fee
),但必须按类别(F、M 或 O)分组。
我尝试了以下查询,但我遇到了如下所示的错误:
SELECT
(fine_amount + court_fee) as [Total Fines Owed]
FROM
crime_charges
INNER JOIN
crimes on crimes.crime_id = crime_charges.crime_id
GROUP BY
classification;
这是我得到的错误:
消息 8120,第 16 层,状态 1,第 74 行
列“crime_charges.court_fee”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
您可能想将罚款金额加上法庭费用相加:
select classification, sum(fine_amount+court_fee) as [Total Fines Owed]
from crime_charges cc
inner join crimes c on c.crime_id = cc.crime_id
group by classification;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.