My table looks something like this:
|-----------------------------------------------------|
|unit_number sub_rule_id num_donations |
|-----------------------------------------------------|
| 1 5 3 |
| 1 6 2 |
| 2 5 3 |
| 3 7 2 |
| 3 7 2 |
|-----------------------------------------------------|
Each sub_rule_id has a unit_number and num_donations associated with it, and this num_donations must be met in order for the rule to be valid. If that number is not met, the row in the database can be deleted. In the above example, the first three rows would be deleted (ie. sub_rule_id 5 does not appear three times for the same unit). The last two rows would remain, because there are two instances of the sub_rule_id 7 for the same unit number. In trying to code this (the table's name is #result), I came up with:
delete from
#result
where
#result.current_donation_only = 0 and
(select
count(*)
from
#result rr
where
(#result.unit = rr.unit
and r.sub_rule_id = rr.sub_rule_id) < num_donations
Obviously the syntax is wrong...help please?
|-----------------------------------------------------|
|unit_number sub_rule_id num_donations |
|-----------------------------------------------------|
| 1 5 3 |
| 1 6 2 |
| 2 5 3 |
| 3 7 2 |
| 3 7 2 |
|-----------------------------------------------------|
Each sub_rule_id has a unit_number and num_donations associated with it, and this num_donations must be met in order for the rule to be valid. If that number is not met, the row in the database can be deleted. In the above example, the first three rows would be deleted (ie. sub_rule_id 5 does not appear three times for the same unit). The last two rows would remain, because there are two instances of the sub_rule_id 7 for the same unit number. In trying to code this (the table's name is #result), I came up with:
delete from
#result
where
#result.current_donation_only = 0 and
(select
count(*)
from
#result rr
where
(#result.unit = rr.unit
and r.sub_rule_id = rr.sub_rule_id) < num_donations
Obviously the syntax is wrong...help please?