Jump to content

Simple SQL COUNT question


johnsmith153

Recommended Posts

SELECT COUNT(*) obviously counts all records matching the WHERE clause,

 

however...

 

Field1 / Field2

1 / A

  / A

1 / B

1 / A

 

I want to count all records where Field2 is "A", but if Field1 is set to "1" then that record counts as half. So above would return a result of 2 (one and two halfs = 2)

 

I'm sure SQL can do this without the need to perfrom PHP calculations.

 

Hope this makes sense. Thanks in advance.

Link to comment
Share on other sites

I believe what you are looking for is COUNT(IF()).

 

COUNT(IF(Field1 = 1, 0.5, 1)) WHERE Field2 = 'A'

 

This has not been tested, but something like it should work.

 

You may need the SUM() function (instead of COUNT) for what you are looking for.

 

If the field is literally null, IFNULL may be faster.

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.