I have two tables: Coupons and Responses.
The Coupons table includes fields such as:
- VendorID [key]
- CouponID [key]
- CouponDescription
The Responses table includes fields such as:
- RespondentID [key]
- CouponID [key]
- (VendorID not repeated in this table.)
- Accepted [true/false]
- Rejected [true/false]
- Rating [1 to 5]
When someone accepts a coupon, they eventually rate it in the Responses table. Likewise, when someone rejects a coupon, the rejection appears in the Responses table.
When fetching a list of available coupons from the Coupons table, I'd like to exclude vendors who previously received two or more rejections from a respondent. For instance if RespondentID 3 previously rejected two or more coupons from Vendor 47, all coupons from Vendor 47 would no longer be shown to RespondentID 3.
Two things make it difficult for a SQL novice like myself.
The first is how to write the subquery that counts rejections from a particular vendor and by a particular respondent.
The second is how to join-back (so to speak) the Responses table to the Coupons table so that the sub-query of the Responses table bases its result on the RespondentID (which is in the Responses table) and the VendorID (which is not in the Responses table and must be determined based on the CouponID (which is in both tables).
Thanks for your assistance. It's always appreciated.