Jump to content

help with GROUP BY


schilly

Recommended Posts

Here is one query I have that works:

SELECT company,

  retail_vendors.id as retail_id,

  COUNT(retail_codes.id) as coupons_assigned,

  SUM(IF(redeemed=1,1,0)) as coupons_used,

  SUM(IF(shipped=1,1,0)) as shipped,

  SUM(IF(redeemed=0,1,0)) as coupons_left

FROM retail_vendors LEFT JOIN retail_codes ON retail_vendors.id = vendor

WHERE status = 1

GROUP BY retail_id ORDER BY company

 

Now I want to select vendors with <= 5 coupons left. I'm trying this:

SELECT company, retail_vendors.id as retail_id,

SUM(IF(redeemed=0,1,0)) as coupons_left

FROM retail_vendors LEFT JOIN retail_codes ON retail_vendors.id = vendor

WHERE SUM(IF(redeemed=0,1,0)) <= 5 GROUP BY retail_vendors.id ORDER BY company

 

but I keep getting an error: "invalid use of group function". What's the issue here or is there a better way to do this? I tried WHERE coupons_left <= 5 and that didn't work either.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.