![](/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.