Reputation: 3563
Trying to make a smaller sample database but still have the data be somewhat statistically relevant. How can I delete x % of rows from the table ? Been fooling around with the NEWID() function.
Upvotes: 7
Views: 1786
Reputation: 69554
DELETE
FROM TABLE_NAME
WHERE PK IN (SELECT TOP (75) PERCENT PK
FROM TABLE_NAME
ORDER BY NEWID())
Suggestion by Martin Smith
DELETE T
FROM (SELECT TOP (75) PERCENT *
FROM TABLE_NAME
ORDER BY NEWID()) T
Upvotes: 10
Reputation: 320
My working example on Azure SQL Database (most syntax compatible with SQLServer):
delete TOP (75) PERCENT
FROM outer_sources.del_me_tbl
Upvotes: 1
Reputation: 115630
Another way would be:
DELETE
FROM table_name
WHERE ABS(CHECKSUM(NEWID())) % 100 < 75 ;
It will not delete exactly 75% of the rows but it will be faster for a bigger table, as it won't need to do a sort.
Upvotes: 4