Solved

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

Posted on 2014-12-15
4
312 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
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 51

Accepted Solution

by:
HainKurt 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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