2016-07-06 1 views
-2

タイトルに基づいてtotal_amountを合計する必要があります。実際に私はこれらの2つのテーブルの間に関係はありませんが、貯蓄を取り出すために、私はそれらを合計し、両方の合計の合計を差し引く必要があります。私はtotal_Amountを合計する必要があります。title = incomeとtitle =経費

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

期待するIDを示す@expenseにエラーを返します – abcd

+0

私はこの言語を一度も使用していないので、誤った仮定をしている可能性があります。私は質問を修正しました - あなたはこれを試すことができますか? – AdamRossWalker

+0

SQL Server 2012を使用しています – abcd

関連する問題