?
Solved

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

Posted on 2014-12-15
4
Medium Priority
?
344 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 58

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 58

Accepted Solution

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 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