Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server sql server结果的两倍和三倍_Sql Server_Vb.net - Fatal编程技术网

Sql server sql server结果的两倍和三倍

Sql server sql server结果的两倍和三倍,sql-server,vb.net,Sql Server,Vb.net,我想创建一个特定的表,但是一些数值在结果中是两倍或三倍的。 情况如下: 表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 paper

我想创建一个特定的表,但是一些数值在结果中是两倍或三倍的。 情况如下:

表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)
select sum(P.Income) as 'Income from Payments',
sum(E.expense) as 'Expenses',
sum(P.Income)-sum(E.expense) as 'Net Profit',
DateName( month , DateAdd( month , IncomeMonth , 0 ) - 1 ) as 'Month'
from
(select sum(payments.amountpaid) as Income,
month(DOP) as IncomeMonth
from payments group by month(dop)) as P,
(select sum(expenses.amountpaid) as Expense,
month(timestamp) as ExpenseMonth
from expenses 
group by month(timestamp))
as E
where
E.Expensemonth=P.IncomeMonth
group by P.IncomeMonth
如查询所示:我的表格应显示每月和每年的付款、费用和净
利润=付款-费用之和

问题是,当得到结果时,总金额(费用。支付金额)总是加倍


因此,任何想法…

听起来都需要指定两个表之间的关系

我想是这样的:

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 
WHERE PURCHASES.DOP = EXPENSES.TIMESTAMP /*Add this*/
group by datename(month,timestamp),datepart(year,timestamp)

我解决了,伙计们,经过4个小时的尝试,问题是:

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)
select sum(P.Income) as 'Income from Payments',
sum(E.expense) as 'Expenses',
sum(P.Income)-sum(E.expense) as 'Net Profit',
DateName( month , DateAdd( month , IncomeMonth , 0 ) - 1 ) as 'Month'
from
(select sum(payments.amountpaid) as Income,
month(DOP) as IncomeMonth
from payments group by month(dop)) as P,
(select sum(expenses.amountpaid) as Expense,
month(timestamp) as ExpenseMonth
from expenses 
group by month(timestamp))
as E
where
E.Expensemonth=P.IncomeMonth
group by P.IncomeMonth

表之间没有关系吗?e、 g.假设两行费用(2012年1月)(2012年2月)加上4行付款将产生8行视图(从费用、采购)它们之间没有关系如果Nick不对(我认为他应该是对的),那么在不改变设计的情况下,您的问题就没有解决方案。@AliMkahal如果您的问题得到解决,然后别忘了接受有用的答案。这没有关系,只是我需要计算每个月内的所有采购和费用,然后从每个月获得(采购费用)。虽然有关系。。。月份和年份。必须将每个月的购买与每个月的支出分别关联起来-否则,您无法按月正确地分解数据。如果您没有从该查询中返回任何内容,可能是因为您的DOP和Timestamp列不完全匹配(甚至到毫秒),因此,您可以将日期设置为月份,也可以使用:WHERE YEAR(DOP)=YEAR(Timestamp)和month(DOP)=month(Timestamp)列“T.Income”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。其中P.ID=E.ID是不必要的,因为不需要在两者之间建立关系tables@AliMkahal
其中P.ID=E.ID不是不必要的
dude,这是获得双精度值的唯一原因。如果不使用它,它将添加冗余值。明白了吗?没有关系,也不需要关系。我会尽量多解释的。假设表“付款”有:-记录:100$11-10-2012-记录:150$12-10-2012-记录:140$12-11-2012-记录:120$10-12-2012,因此,我们这里的总数为,第10个月250$11个月140$12个月120$12个月。同样的概念也适用于费用,然后我希望结果表显示一个月的列表,以及每个月下一个月的总费用。正如你所总结的,没有必要建立关系。