Jump to content

PHP MSSQL COUNT per Year


Call-911

Recommended Posts

Hello All,

 

I need to count the number of SQL date entries we have per year. I can coun this by:

 

SELECT COUNT(*) FROM news WHERE show='true' AND date >= '2011-07-01'

 

But how can I make it change years automatically. Our fiscal year starts July 1, and I'd like it to restart the count automatically every year. Does that make sense?

Link to comment
Share on other sites

You have a field where you put the string value of "true"? If the only value is true or false you should be using a tinyint type field and storing a 0 or 1.

 

SELECT COUNT(*)
FROM `news`
WHERE `show` = 'true'
  AND YEAR(`date`) = YEAR(NOW())

Link to comment
Share on other sites

For the year start/end -

SELECT COUNT(*) FROM news WHERE `show`='true' AND IF(CURDATE() >= CONCAT(YEAR(CURDATE()),'-07-01'),
date BETWEEN CONCAT(YEAR(CURDATE()),'-07-01') AND CONCAT(YEAR(CURDATE())+1,'-06-30'),
date BETWEEN CONCAT(YEAR(CURDATE())-1,'-07-01') AND CONCAT(YEAR(CURDATE()),'-06-30'))

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.