Jump to content

Count if type = 42


mentalist

Recommended Posts

I have a table with a column named "type", I'm after counting the rows with type 40 - 45 as one count and also counting the rows of type 13 say. Whether this is done using group or what ever I don't mind, but is it possible in a single call? (* There is a where in there too, but)

 

Cheers

Link to comment
Share on other sites

Sure you can compute a value and group on the computed column.

 

SELECT IF(type >39 AND type <46, 40, type) types, count(*) FROM foo GROUP BY types;
To omit values other than 13 and your range just add a where clause.

 

SELECT IF(type >39 AND type <46, 40, type) types, count(*) 
FROM foo 
WHERE type = 13 OR type BETWEEN 40 AND 45
GROUP BY types;
Link to comment
Share on other sites

If you try and alias the name of the computed column back to the same name as the original column, it won't work correctly. Your computed name (and the name you use in the GROUP BY) have to be the computed name.

 

I'm not sure if there's a way to finesse this issue, although Barand might know.

 

With that said, it doesn't explain your result. To get only one group, there seems to be something wrong with your GROUP BY or your WHERE clause.

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.