Your problem can be solved with Dynamic Pivoting. Please read this article.
try it
DECLARE @t TABLE(Id_Contract INT, Dt DATETIME,Amount INT) INSERT INTO @t SELECT 1,'2012-01-01 00:00:00.000',500 INSERT INTO @t SELECT 1,'2012-03-01 00:00:00.000',450 INSERT INTO @t SELECT 2,'2012-09-01 00:00:00.000',300 INSERT INTO @t SELECT 3,'2012-08-01 00:00:00.000',750 DECLARE @cols AS VARCHAR(MAX), @query AS VARCHAR(MAX); SELECT Id_Contract , LEFT(DATENAME(month,Dt),3) + ' ' + DATENAME(Year,Dt) AS Month_Year_Name ,Amount INTO
//Result
Id_Contract Jan 2012 Mar 2012 1 500 450
Edit
For your test data
DECLARE @t TABLE(Id_Contract INT, Dt DATETIME,Amount INT) INSERT INTO @t SELECT 1,'2012-01-01 00:00:00.000',500 INSERT INTO @t SELECT 1,'2012-03-01 00:00:00.000',450 INSERT INTO @t SELECT 2,'2012-03-01 00:00:00.000',450 INSERT INTO @t SELECT 3,'2012-08-01 00:00:00.000',750
output
Id_Contract Jan 2012 Mar 2012 1 500 450 2 NULL 450
Let me know if this meets the requirements.
source share