-- -- Sample vs Sample -- DROP TABLE ResultsR1sR2 CREATE TABLE [dbo].[ResultsR1sR2] ( [tid1] int NOT NULL, [tid2] int NOT NULL, [sim] float NOT NULL, [S] int NOT NULL, PRIMARY KEY (S,tid1,tid2), FOREIGN KEY (tid1) REFERENCES R1, FOREIGN KEY (tid2) REFERENCES R2, ) GO DECLARE @S int DECLARE @I int DECLARE @UPPERLIMIT int -- The upper limit in the sample size will be 2^@UPPERLIMIT SET @I=0 SET @UPPERLIMIT=8 WHILE @I <= @UPPERLIMIT BEGIN SET @S=POWER(2, @I) BEGIN TRANSACTION INSERT INTO ResultsR1sR2(tid1,tid2,sim,S) SELECT R1Sample.tid AS tid1, R2Sample.tid AS tid2, SUM(R1Sample.c * R2Sample.c * R1sum.total * R2sum.total) / (@S * @S) AS sim, @S AS S FROM R1Sample, R2Sample, R1Sum, R2Sum WHERE R1Sample.token = R1sum.token AND R2Sample.token = R2Sum.token AND R1Sample.token = R2Sample.token AND R1Sample.S = R2Sample.S AND R1Sample.S = @S GROUP BY R1Sample.tid, R2Sample.tid COMMIT TRANSACTION CHECKPOINT DBCC SHRINKDATABASE(ICDEQ2) DBCC SHRINKDATABASE(ICDEQ3) SET @I = @I+1 END GO