Hi
What you are looking for is crosstab report which converts rows to columns. MS SQL 2005 introduced PIVOT but in your case it may not work properly as you need to dispaly unknown values which comes from a 3rd table. For your convenience, this is URL that you can refer to
Stored procedure: A simple way to perform crosstab operations
Once you download the sp_CrossTab (at the bottom of the page), put it in the Master database. Then use the following script to test it.
IF OBJECT_ID('tempdb..#account') IS NOT NULL
drop table #account
create table #account
(
id bigint identity(1,1),
AccountName varchar(50),
PurchaseDate datetime not null,
PurchaseAmount float not null
)
insert #account Values('Bill Gates', '2009-01-11', 300)
insert #account Values('David Jung', '2009-01-11', 100)
insert #account Values('Steve Jobs', '2009-01-11', 200)
insert #account Values('Tom Hanks', '2009-01-21', 300)
insert #account Values('Julia Roberts', '2009-01-21', 100)
insert #account Values('Mel Gibson', '2009-01-21', 200)
EXECUTE sp_Crosstab
'select AccountName,PurchaseAmount,datepart(year,PurchaseD ate) [PurcharYear] from #account'
,NULL
,NULL
,'AccountName'
,'PurchaseAmount'
,'sum'
It may not directly give you the solution but still it may give you something to try, I hope.