?
Solved

How do I select arbitrary date ranges in a MySQL query

Posted on 2009-07-15
3
Medium Priority
?
306 Views
Last Modified: 2013-12-13
I need a query/code snippet that would allow me to get a date-range from the previous Friday night until now.  I have figured out how to get date results from the last 7 days, but I need them specifically from last Friday.

On a slightly different note, but related to the same project, I need some code that would allow me to get results from each disparate month of the 12 months, starting with a year ago.  For example, I need to return the number of orders within dates of July 1st - the 31st of 2008, August 1st - 31st of 2008, ... up to July 1st - 31st of 2009.  This data will be fed into a dynamically created chart, so it has to all move along dynamically with the current date.
0
Comment
Question by:russianryebread
[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
  • 3
3 Comments
 
LVL 14

Accepted Solution

by:
racek earned 500 total points
ID: 24859533
weekday 4 is friday ...

SELECT *
FROM  your_table
WHERE you_date_column > (SELECT  DATE_SUB(MAX(you_date_column), INTERVAL 7 days)
                                             FROM your_table  where WEEKDAY(you_date_column) = 3 )  ;
0
 
LVL 14

Expert Comment

by:racek
ID: 24859622
or faster solution ...
SELECT *
FROM  your_table
WHERE LEFT(your_date_column,10) 
    = LEFT(CASE WEEKDAY(NOW()) 
   WHEN 4 THEN DATE_SUB(your_date_column, INTERVAL 7 days)
   WHEN 5 THEN DATE_SUB(your_date_column, INTERVAL 8 days)
   WHEN 6 THEN DATE_SUB(your_date_column, INTERVAL 9 days)
   WHEN 3 THEN DATE_SUB(your_date_column, INTERVAL 6 days)
   WHEN 2 THEN DATE_SUB(your_date_column, INTERVAL 5 days)
   WHEN 1 THEN DATE_SUB(your_date_column, INTERVAL 4 days)
   WHEN 0 THEN DATE_SUB(your_date_column, INTERVAL 3 days)
END , 10);

Open in new window

0
 
LVL 14

Assisted Solution

by:racek
racek earned 500 total points
ID: 24859663
second one - use

select
YEAR(your_date_column) as year
MONTH(your_date_column) as month,
SUM(datacol) as datasum
FROM your_table
GROUP by 1,2
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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