2016-07-06 24 views
-2

我需要根據標題總和total_amount。其實我沒有這兩張表之間的關係,但要拿出我需要總結他們和儲蓄兩者總和的差異。我需要總和total_Amount標題=收入和標題=費用

ALTER PROCEDURE [dbo].[SpSavingDetail] 
@year varchar(10), 
@month varchar(10) 
AS 
BEGIN 

SET NOCOUNT ON; 

declare @Saving int 

select e.Date_time as Date_time ,c.category as Particular,e.Description as Description ,e.Amount as Amount, 
e.Frequency as Frequency,e.Total_Amount as Total_Amount,'Expense' as Title from tbl_expense as e 
left join tbl_category as c on c.Cat_id=e.Cat_id where datepart(MM,e.Date_time)[email protected] and datepart(YYYY,e.Date_time)[email protected] 
union 
select i.Date_time as Date_time ,s.source as Particular,i.Description as Description ,i.Amount as Amount, 
i.Frequency as Frequency,i.Total_Amount as Total_Amount,'Income' as Title from tbl_income as i 
left join tbl_source as s on s.Source_id=i.Source_id 
where datepart(MM,i.Date_time)[email protected] and datepart(YYYY,i.Date_time)[email protected] 





    [1]: http://i.stack.imgur.com/8Z5po.jpg 
+0

這是怎麼C#代碼? –

+0

其存儲過程,我在報告中使用 – abcd

回答

0

我不知道這種語言,(是T-SQL?如果你更恰當地標記問題,您將有更多的運氣),但你應該能夠沿着下面的線做一些事情:

declare @expense int 
declare @income int 

select @expense = SUM(e.Amount) 
    from tbl_expense as e 
where datepart(MM,e.Date_time)[email protected] and datepart(YYYY,e.Date_time)[email protected] 

select @income = SUM(i.Amount) 
    from tbl_income as i 
where datepart(MM,i.Date_time)[email protected] and datepart(YYYY,i.Date_time)[email protected] 

@Saving = @income - @expense 
+0

它給錯誤,到@expense說期待ID – abcd

+0

我從來沒有使用過這種語言,所以我可能做了一些不正確的假設。我修改了這個問題 - 你可以試試這個嗎? – AdamRossWalker

+0

我使用SQL服務器2012 – abcd