Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to form the query

Author  Topic 

kamal.A
Yak Posting Veteran

70 Posts

Posted - 2008-04-05 : 15:48:42
I have two tables. The table is below.

Table name 1 : Income
Income RentMonth
1500 Jan
1500 Feb
1500 Apr

Table name 2 : Expense
Expense ExpMonth
200 Jan
300 Mar
400 Apr

The result table becomes (Profit=Income-Expense)
Profit Month
1300 Jan
1500 Feb
-300 Mar
1100 Apr

But I form the query by join the both Income and Expense tables to subtract the Income and Expense month wise.

But one moth is in one table the same month is not in another table.

For Example Feb month is in Income table, but not in Expense table.And Mar month is in Expense table and not in Income table. So how will I form the query to achieve my result table as i indicated above. Kindly help me.

Kamal.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-05 : 16:22:12
[code]INSERT INTO ResultTable (Profit,Month)
SELECT ISNULL(i.Income,0)-ISNULL(e.Expense,0),
ISNULL(i.RentMonth,e.ExpMonth)
FROM Income i
FULL OUTER JOIN Expense e
ON e.ExpMonth=i.RentMonth[/code]
Go to Top of Page

kamal.A
Yak Posting Veteran

70 Posts

Posted - 2008-04-07 : 09:59:49
The query run perfectly. Thanks for your valuable help.

Kamal.
Go to Top of Page
   

- Advertisement -