Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Fast Script to Blank Out Part of Table Randomly

$
0
0

The table variable @Matrix contains a matrix (like a chessboard). There are 3 requirements:

1. It has to be random blanking; FILLFACTOR 70 means 70% kept 30% blanked out

2. Row wise the difference between the  highest fill and smallest fill count  should be < 3;  e. g 36 - 34 < 3

3. Same as 2 for columns

Constraint 2 & 3 provide for an even fill meaning there should not be a row with 10 blanks and another with 40 blanks. Similarly there should not be a column with 20 blanks and another with 30 blanks. In the example below the typical fills for rows would be 34, 35, 36 (blanks: 14, 15, 16). Same for columns.

Thanks.

Table:

SET NOCOUNT ON;
DECLARE @FILLFACTOR tinyint = 70, @SIZE tinyint = 50;
DECLARE @i tinyint=1, @j tinyint, @Content nchar(1) = 'A', @Blank nchar(1) = SPACE(1);
DECLARE @Matrix TABLE (ROW tinyint, COL tinyint, CELL nchar(1));
WHILE (@i <= @SIZE) BEGIN 
	SET @j =1; WHILE (@j <= @SIZE) BEGIN INSERT @Matrix VALUES (@i, @j, @Content); SET @j += 1; END    
SET @i += 1; END;
SELECT COUNT(*) FROM @Matrix;
-- 2500

Random coordinate generator:

DECLARE @SIZE tinyint = 50; 
SELECT CEILING((@SIZE ) * RAND(CAST(CHECKSUM(NEWID()) AS VARBINARY)));
GO 1000


Kalman Toth Database & OLAP Architect sqlusa.com
New Book / Kindle: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>