I have a large table and want to create a smaller table that systematically picks out one of every 5 records from the original. How best to do this?
This means it should have every row with id ending in 0 and 5 for example.
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming that your IDs as auto-incremented and continuous (with no deleted records), the simplest way could something like:
You would insert that into the small table like this:
If your primary key ID is not sequential you can do this:
Note though that this is OK for a one-time thing as it is quite slow if you have a lot of records.
Better do it in code if you have lots of records (or maybe you have no primary key, which is usually bad btw).
The code below is generic code to copy all records from one table into another (existing) table:
And call it like that (assuming that both BigTable and SmallTable have the same structure and that there are no existing records in SmallTable that could create a primary key violation when trying to inster duplicate records):
Edit: following HansUp response, changed SQL queries to use
Modinstead of its functional version:(Round(ID/5) = ID/5). He’s right, no need to make it more complex than it needs to be.