将结果总和添加到等式中

时间:2017-03-07 21:18:56

标签: sql sql-server sql-server-2012

我正在尝试添加ard.terms_amount的结果,并从我的“Open AR”计算中减去该结果。所以在这种情况下它应该减去0.90

SELECT 
customer.customer_id
,customer.customer_name


,cast(sum(ih.total_amount - ih.amount_paid ) as decimal (10,2)) as [Open A/R]


,cast(dbo.p21_fn_days_sales_outstanding(customer.company_id, customer.customer_id, 90, GETDATE()) as decimal (10,2)) average_dso 
,terms.terms_desc
,ard.terms_amount



FROM customer
INNER JOIN terms ON terms.terms_id = customer.terms_id
inner join invoice_hdr ih on ih.customer_id = customer.customer_id
inner join ar_receipts_detail ard on ard.customer_id = customer.customer_id
WHERE  customer.company_id = 'ABC' and customer.customer_id='123456'
and ih.approved ='y'


group by customer.customer_id,customer.customer_name,customer.company_id,terms.terms_desc,ard.terms_amount

enter image description here

1 个答案:

答案 0 :(得分:0)

所以你要做的就是获取列' terms_amount'的总和。并从列的总和中减去它' Open A / R'正确?

您可以将列的总和设置为两个变量然后减去两者(请原谅我的语法,有一段时间我使用MSSQL但我最近切换到MySQL工作):

declare @terms_amount float; 
declare @OpenA/R float
set @terms_amount = (select sum(terms_amount) from yourdatabase);
set @OpenA/R = (select sum(OpenA/R) from yourdatabase);
select @terms - @OpenA/R;

我认为语法是正确的,但同样,我暂时没有使用过MSSQL。