Solved

mysql select monthly report - 25th of the prior month thru 24th of current month

Posted on 2014-12-15
4
304 Views
Last Modified: 2014-12-15
MYSQL 5.6.22

  Hello everyone,

     I need help converting a query from MSSQL to MYSQL.  There is a report that needs to run every month on the 25th day that does a count from the 25th of the previous month to the 24th of the current month.

This currently works in MSSQL:

select
(select COUNT (*)
from pacsdb.study
where study_custom1 like '%SITE1%'
and study_datetime >= convert(date, dateadd(month, -1, dateadd(day, 25-day(getdate()), getdate())))
and study_datetime < convert(date, dateadd(day, 25-day(getdate()), getdate()))) as SITE1, 
(select COUNT (*) 
from pacsdb.study
where study_custom1 like '%SITE2%'
and study_datetime >= convert(date, dateadd(month, -1, dateadd(day, 25-day(getdate()), getdate())))
and study_datetime < convert(date, dateadd(day, 25-day(getdate()), getdate()))) as SITE2, 
(select COUNT (*) 
from pacsdb.study
where study_custom1 like '%SITE3%'
and study_datetime >= convert(date, dateadd(month, -1, dateadd(day, 25-day(getdate()), getdate())))
and study_datetime < convert(date, dateadd(day, 25-day(getdate()), getdate()))) as SITE3

Open in new window


  I know that comparing MSSQL to MYSQL is apples & oranges, but I need help with converting the date calculation to function in MYSQL.

The statement above should just come back with three counts, one for each site.  

This will always run on the 25th day of each month.

thanks
0
Comment
Question by:doc_jay
  • 2
  • 2
4 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
maybe this:

where
logdate > date_add(DATE(NOW()), interval -1 month) - 1 and
logdate > date_add(DATE(NOW()), interval -1 day)

or

where
logdate > date_add(date_add(DATE(NOW()), interval -1 month), interval -1 day) and
logdate > date_add(DATE(NOW()), interval -1 day)
0
 

Author Comment

by:doc_jay
Comment Utility
Thanks.  This does work as it always goes back one month.  How could it be written if I needed to run this on a different date to ensure that it always counted between the 25th of the prior month & the 24th of the current month?
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
maybe this: DATE_FORMAT(NOW() ,'%Y-%m-XX')

where XX is hardcoded to 15, 20, 22 etc...

sample:

where
logdate > date_add(DATE_FORMAT(NOW() ,'%Y-%m-24'), interval -1 month) and
logdate < DATE_FORMAT(NOW() ,'%Y-%m-24')
0
 

Author Comment

by:doc_jay
Comment Utility
thanks, this is just what I needed.

I had to change it a bit to get the 25th - the 24th

logdate > date_add(DATE_FORMAT(NOW() ,'%Y-%m-25'), interval -1 month) and
logdate < DATE_FORMAT(NOW() ,'%Y-%m-25')

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now