Solved

How do I select arbitrary date ranges in a MySQL query

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

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

688 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