Solved

How do I select arbitrary date ranges in a MySQL query

Posted on 2009-07-15
3
299 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

820 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