|
Question : TOP Percentage puzzle with SQL
|
|
I know how to collect and sort a collection of records from a large database via SQL.
Once done I would like to automate things so that only the top 10% of this record subset would be kept as my final sorted record collection (i.e. if 920 records were located, only the first 92 would be kept).
This would be real easy if I had a sequential set of numbers (1,2,3,4,5,6,...,920) to exercise the TOP command on once this subset set of records is found.
I have multiple subsets with in my table. The first subset may be 920 records, the second may be 150 records, and so on. The Select Top 10 Percent will not work because of the multiple sets.
Does anyone know how to accomplish this task? I have a rudimentary understanding of SQL.
|
|
Answer : TOP Percentage puzzle with SQL
|
|
let's say you have this data:
Field1 Field2 Field3 Field4 a a a a a a a a a a a a a a a a b b b b b b b b b b b b b b b b b b b b b b b b c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c
that's 4 rows of a's, 6 rows of b's and 10 rows of c's (this just mimics the type of data you have)
now with no unique identifier, i can't tell one record from the other. but if i add an autonumber i get a table like:
Field1 Field2 Field3 Field4 ID a a a a 1 a a a a 2 a a a a 3 a a a a 4 b b b b 5 b b b b 67 b b b b 7 b b b b 8 b b b b 9 b b b b 10 c c c c 11 c c c c 12 c c c c 13 c c c c 14 c c c c 15 c c c c 16 c c c c 17 c c c c 18 c c c c 19 c c c c 20
(sorry if the columns don't line up)
NOW, if i want to delete the top 50 percent of all these unique groups (here, there are THREE unique groups)
i would run 3 action queries that delete the top 50 percent from each set. like this:
DELETE * FROM MyTable WHERE ID IN ( SELECT TOP 50 PERCENT ID FROM MyTable WHERE Field1 = 'a' and Field2 = 'a' and Field3 = 'a' and Field4 = 'a' )
i would repeat the steps for b and c and i would delete a total of 10 rows (2a, 3b, 5c)
starting to make sense?
i'll post this sample db on my website...
dovholuk
|
|
|
|