QQQ. First Trading Day of the month, close to close.

Profit/loss (%) calculated for each first trading day of the month for the period from 3/10/1999 till 11/28/2011: go long on close, exit on the next day close.

year123456789101112Total
19990.000.000.002.02-0.55-2.270.68-0.300.710.03-0.532.121.91
20003.562.600.81-6.370.904.871.90-1.360.98-2.31-0.861.606.32
2001-9.251.302.78-4.683.972.87-0.553.12-3.26-0.874.07-1.74-2.24
20022.99-0.985.481.90-0.53-3.67-5.03-4.91-3.785.742.771.010.99
20034.060.17-2.070.820.87-0.701.34-1.091.422.201.631.4510.10
2004-0.26-0.281.290.860.95-0.14-1.880.180.732.660.391.986.48
2005-1.070.301.10-1.000.410.81-0.140.16-0.130.26-0.081.822.44
20062.170.371.330.12-0.992.300.61-1.720.53-1.28-1.39-0.871.18
20070.19-0.16-0.480.140.330.060.900.771.631.14-1.90-0.841.78
2008-1.601.01-0.654.103.06-1.241.40-1.30-1.26-1.06-0.22-8.14-5.90
20094.141.18-3.371.490.272.770.061.47-1.91-3.070.421.014.46
20101.441.101.430.001.38-0.94-0.281.842.89-0.120.082.0610.88
20111.551.85-1.630.05-0.19-2.221.49-0.47-0.92-2.64-2.680.00-5.81

Query used:
select [year], [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13] Total into #t
from
	(	select cast((t.c - t_1.c)/t.c * 100 as decimal(10,2)) pl, year(t.d) [year], MONTH(t.d) [month] 
		from qqq t
		join qqq t_1 on t_1.id = t.id - 1
		where month(t_1.d)%12 = MONTH(t.d) - 1)	p
pivot
(
sum(pl)
for [month] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13]) 
)	r	
order by [year]


--2
update #t
set Total = coalesce([1], 0) + coalesce([2], 0) + coalesce([3], 0) + coalesce([4], 0) 
			+ coalesce([5], 0) + coalesce([6], 0) + coalesce([7], 0) + coalesce([8], 0) 
			+ coalesce([9], 0) + coalesce([10], 0) + coalesce([11], 0) + coalesce([12], 0) 


DECLARE @HeaderList varchar(max), @head varchar(1000)
SELECT @HeaderList = COALESCE(@HeaderList + '', '') +    name 
from tempdb.sys.columns where object_id = object_id('tempdb..#t') order by column_id
select @head = '' + @HeaderList + ''

DECLARE @SelectList varchar(max)
SELECT @SelectList = COALESCE(@SelectList + '+''''+', '') +    'CAST([' + name + '] AS varchar(50))'
from tempdb.sys.columns where object_id = object_id('tempdb..#t') order by column_id

declare @sql varchar(max)
select @sql = 'select ''' + @head + ''' union all select ''''+' + @SelectList + '+'''' from #t'
exec (@sql)



theCrunchR | © 2011