Solved

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

Posted on 2014-12-15
4
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 40500956
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
ID: 40501022
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 55

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 40501068
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
ID: 40501305
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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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