SELECT TOP 10 RAND(ID) PERC, ID, N1, N2
FROM
A Software Engineer and data professional's blog on SQL, data, databases, data architectures, data management, programming, Software Engineering, Project Management, ERP implementation and other IT related topics.
-- cleaning the table DROP TABLE IF EXISTS dbo.LottoExtracts --creating the LottoExtracts table CREATE TABLE dbo.LottoExtracts( DrawingDate date NULL , N1 smallint NULL , N2 smallint NULL , N3 smallint NULL , N4 smallint NULL , N5 smallint NULL , N6 smallint NULL)
--creating random data DECLARE @index, @counter int DECLARE @N1, @N2, @N3, @N4, @N5, @N6 smallint SET @index = 0 WHILE @index < 2000 BEGIN -- creating the first number SET @N1 = Cast(1000 * RAND() as int) % 48 + 1 -- creating the second number SET @counter = 1 WHILE @counter < 50 or @N1 = @N2 BEGIN SET @N2 = Cast(1000 * RAND() as int) % 48 + 1; SET @counter = @counter + 1 END; -- creating the third number SET @counter = 1 WHILE @counter < 50 or @N3 IN (@N1, @N2) BEGIN SET @N3 = Cast(1000 * RAND() as int) % 48 + 1 SET @counter = @counter + 1 END -- creating the fourth number SET @counter = 1 WHILE @counter < 50 or @N4 IN (@N1, @N2, @N3) BEGIN SET @N4 = Cast(1000 * RAND() as int) % 48 + 1 SET @counter = @counter + 1 END -- creating the fifth number SET @counter = 1 WHILE @counter < 50 or @N5 IN (@N1, @N2, @N3, @N4) BEGIN SET @N5 = Cast(1000 * RAND() as int) % 48 + 1 SET @counter = @counter + 1 END -- creating the sixth number SET @counter = 1 WHILE @counter < 50 or @N6 IN (@N1, @N2, @N3, @N4, @N5) BEGIN SET @N6 = Cast(1000 * RAND() as int) % 48 + 1 SET @counter = @counter + 1 END --inserting a new drawing INSERT LottoExtracts(DrawingDate, N1, N2, N3, N4, N5, N6) VALUES (DateAdd(d, @index*7, '01/01/2000'), @N1, @N2, @N3, @N4, @N5, @N6) SET @index = @index + 1 END
--checking the data SELECT * FROM dbo.LottoExtracts
-- creating first number SET @N1 = Cast(1000 * RAND() as int) % 48 + 1
-- creating third number SET @counter = 1 WHILE @counter < 50 or @N3 IN (@N1, @N2) BEGIN SET @N3 = Cast(1000 * RAND() as int) % 48 + 1 SET @counter = @counter + 1 END
-- creating the test table CREATE TABLE Table1( ID int NOT NULL , [Year] int NOT NULL , Product varchar(30) NOT NULL , Price decimal(13,2) NOT NULL , Quantity decimal(13,3) NOT NULL) -- dropping the test table --DROP TABLE Table1 -- deleting the data --TRUNCATE TABLE Table1
-- inserting the data DECLARE @index int SET @index = 1 WHILE @index < 99 BEGIN INSERT Table1(ID, [Year], Product, Price, Quantity) VALUES (@index, 2000 + (@index % 4), 'Product ' + Cast(@index%10 as char(3)), 10 * RAND(), 100 * RAND()) SET @index = @index + 1 END --testing the output SELECT * FROM Table1 -- using the data SELECT [Year], Product, SUM(Price * Quantity) Amount FROM Table1 GROUP BY [Year], Product ORDER BY [Year], Product