Expert 2GB |
Try this: - declare @IncomeTable table (yr int, mon varchar(2), income money, src varchar(10))
-
-
insert into @IncomeTable values(2008, '06', 100.00, 'source1')
-
insert into @IncomeTable values(2008, '06', 80.00, 'source2')
-
insert into @IncomeTable values(2008, '06', 200.00, 'source1')
-
insert into @IncomeTable values(2008, '06', 100.00, 'source2')
-
-
select source1.yr, source1.mon, source1.source_income - source2.source_income
-
from
-
(select yr, mon, src, SUM(income) as source_income
-
from @IncomeTable
-
where src = 'source1'
-
group by yr, mon, src) source1
-
inner join
-
(select yr, mon, src, SUM(income) as source_income
-
from @IncomeTable
-
where src = 'source2'
-
group by yr, mon, src) source2 on
-
source1.yr = source2.yr and source1.mon = source2.mon
-- CK
| |