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

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 open.

year123456789101112Total
19990.000.000.002.82-0.31-0.180.320.150.62-1.370.001.093.14
20005.01-0.190.47-1.611.032.430.070.411.341.66-1.731.6910.58
20010.300.37-1.020.050.141.79-0.112.05-0.31-0.431.57-0.903.50
20021.66-0.301.07-0.70-0.16-0.14-0.52-0.44-1.900.90-0.732.461.20
20031.430.300.410.78-0.040.97-0.81-0.360.310.580.710.935.21
20040.570.340.28-0.120.65-0.78-0.19-0.84-0.280.880.230.371.11
20050.410.220.410.700.35-0.140.220.130.080.21-0.240.653.00
20060.58-0.620.300.540.170.260.32-0.920.53-0.130.34-0.091.28
20070.680.44-1.860.330.020.370.24-0.300.130.08-0.64-0.42-0.93
20080.100.81-0.051.560.04-0.18-0.800.111.59-0.18-0.25-2.180.57
20090.00-0.99-1.54-1.26-0.031.050.501.04-0.36-0.390.100.74-1.14
20101.250.260.450.420.43-0.380.311.241.440.830.371.458.07
20110.930.750.430.500.15-0.220.051.190.25-0.86-2.530.000.64

Query used:
select [year], [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13] Total into #t
from
	(	select cast((t.o - t_1.c)/t.o * 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