我想创建一个特定的表,但一些数值在结果中增加了一倍或三倍。情况如下:
2 表:付款和费用
[Payments]: ID, studentID, Amount, DOP (a row in this table is a payment which a student pays it on DOP (date).
[Expenses]: ID, AmountPaid, TimeStamp (a row in this table is an expense bought such as papers or pens... on a specific date(timestamp)
我的查询是:
select
sum(purchases.amount) as 'Income From Students',
sum(Expenses.amountpaid) as 'Expenses',
sum(purchases.amount-expenses.amountpaid) as 'Net Profit',
datename(month,timestamp) as 'Month',
datepart(year,timestamp) as 'Year'
from expenses,purchases
group by datename(month,timestamp),datepart(year,timestamp)
如查询所示:我的表格应显示每个月和每年的付款、费用和净额的总和profit=payments - expenses
。
问题是,当得到结果时, sum(expenses.amountpaid) 总是加倍。
所以任何想法......