Solved

How do I select arbitrary date ranges in a MySQL query

Posted on 2009-07-15
3
300 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 125 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 125 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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