QQQ. Days of the month, close to close.

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

day1999200020012002200320042005200620072008200920102011Total
12.949.9317.865.715.706.082.89-0.29-0.170.020.856.39-4.3153.56
25.54-2.83-18.42-12.8211.692.483.47-1.662.30-7.763.618.39-3.63-9.64
36.1110.539.68-8.433.32-4.04-2.251.76-1.00-0.585.104.952.1727.34
43.06-3.811.540.61-0.301.521.035.774.15-7.618.78-6.34-3.624.75
59.950.4512.55-1.39-4.992.94-1.82-0.352.010.711.674.952.4729.14
6-2.22-15.24-6.095.003.40-3.17-0.29-0.314.51-13.153.62-4.432.11-26.28
71.9522.95-5.10-9.373.240.542.80-5.53-4.61-6.46-9.23-1.532.74-7.58
80.16-5.87-9.3016.17-2.73-4.03-1.361.980.906.133.773.33-5.733.39
93.22-4.86-12.64-15.22-1.22-0.780.75-2.43-6.06-7.654.572.91-1.22-40.61
102.25-21.6710.272.68-1.260.81-2.160.24-0.80-1.103.639.27-2.36-0.17
117.013.8411.260.114.532.611.44-1.45-0.69-5.970.92-0.965.5728.25
12-3.67-16.627.591.576.402.370.43-2.822.55-1.842.19-0.492.540.20
13-2.1610.76-0.62-1.923.41-1.98-0.24-0.115.0923.310.302.622.3940.83
14-6.13-8.05-3.9513.724.40-2.170.315.66-0.75-11.34-1.240.253.23-6.07
153.470.58-4.9611.79-0.91-2.78-1.527.64-2.72-15.172.143.701.783.02
169.774.71-9.95-1.864.38-0.50-1.411.490.7816.174.22-1.94-5.4820.39
175.2211.92-9.128.79-5.653.521.77-4.11-0.36-11.68-2.393.49-3.64-2.28
182.65-8.4411.41-9.678.874.590.00-0.841.804.476.491.10-5.0017.40
19-6.611.3312.45-13.70-5.32-5.130.601.66-1.57-4.190.08-1.45-1.65-23.54
201.84-10.00-21.31-2.320.190.93-2.88-2.893.19-2.83-8.15-0.72-0.06-44.96
2112.12-3.62-0.730.732.610.432.24-4.936.36-3.377.12-0.732.3720.54
22-5.317.9210.03-7.320.340.70-2.77-1.910.77-11.63-2.361.36-5.32-15.51
230.16-3.45-9.42-4.773.67-2.160.10-1.361.70-0.747.33-2.842.11-9.62
243.003.0510.711.02-12.021.900.123.301.128.274.470.814.2129.95
252.40-4.261.51-0.660.056.941.143.780.030.681.651.72-2.1612.86
26-0.57-2.79-1.67-5.620.510.690.813.02-0.43-0.874.49-1.545.521.54
27-0.640.773.01-3.433.461.06-2.76-2.34-4.61-2.09-5.503.464.62-4.95
285.84-11.44-14.23-5.338.39-2.62-1.110.80-0.569.684.35-4.260.77-9.74
295.69-15.37-6.36-1.726.193.99-0.215.604.86-9.870.80-4.392.58-8.19
30-4.255.73-8.363.48-3.95-3.060.42-1.100.597.49-5.02-5.91-0.13-14.08
31-0.5015.70-3.56-3.98-3.05-0.232.92-0.170.753.02-1.36-1.150.729.14


Query used:
--1
select [day], [1999], [2000], [2001], [2002], [2003], [2004], [2005], [2006],[2007], [2008], [2009], [2010], [2011], [2012] Total into #t
from 
	(	select cast((t.c- t_1.c)/t_1.c * 100 as decimal(10,2))  pl, DATEPART(d, t.d) [day], YEAR(t.d) y 
		from qqq t
		join qqq t_1 on t_1.id = t.id -1
		) p
pivot
(
sum(pl)
for y in ([1999], [2000], [2001], [2002], [2003], [2004], [2005], [2006],[2007], [2008], [2009], [2010], [2011],[2012])
) r	
order by [day]

--2
update t
set t.Total = p.pl
from #t t
join  (
	select
      datepart(d, t.d) [day], 
      cast(sum((t.c- t_1.c)/t_1.c * 100) as decimal(10,2)) 'PL'  
      from qqq t
      join qqq t_1 on t_1.id = t.id -1
      group by   datepart(d, t.d)
      ) p on T.[day] = p.[day]

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


--4
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
--select '' + @SelectList + ''
declare @sql varchar(max)

select @sql = 'select ''''+' + @SelectList + '+'''' from #t'

exec (@sql)



3 and 4 can be replaced with:

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