Here is a query I wrote up to give you the most recent thursday and the ending wednesday
SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
Here is an example for today, 2011-09-21
mysql> SELECT
-> thuwk_beg + INTERVAL 0 second thu_beg,
-> thuwk_beg + INTERVAL 604799 second wed_end
-> FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
-> FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
-> FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
+---------------------+---------------------+
| thu_beg | wed_end |
+---------------------+---------------------+
| 2011-09-15 00:00:00 | 2011-09-21 23:59:59 |
+---------------------+---------------------+
1 row in set (0.00 sec)
Just replace the NOW() function calls with whatever datetime you like and you will have the week starting Thursday all the time for the give datetime you choose.
Here is another example using the specific date '2011-01-01'
mysql> SELECT
-> thuwk_beg + INTERVAL 0 second thu_beg,
-> thuwk_beg + INTERVAL 604799 second wed_end
-> FROM (SELECT (DATE('2011-01-01') - INTERVAL daysbacktothursday DAY) thuwk_beg
-> FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
-> FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE('2011-01-01') dt) AAAA) AAA) AA) A;
+---------------------+---------------------+
| thu_beg | wed_end |
+---------------------+---------------------+
| 2010-12-30 00:00:00 | 2011-01-05 23:59:59 |
+---------------------+---------------------+
1 row in set (0.00 sec)
Your query of table
referencing today would resemble something like this:
SELECT * from `table`,
(SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A) M
WHERE `date` >= thu_beg
AND `date` <= wed_end;
Give it a Try !!!
UPDATE 2011-09-22 16:27 EDT
This was my proposed query for marking Thu-Wed.
SELECT thuwk_beg + INTERVAL 0 second thu_beg,
thuwk_beg + INTERVAL 604799 second wed_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) thuwk_beg
FROM (SELECT SUBSTR('3456012',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
How about other weeks ???
(SELECT SUBSTR('6012345',wkndx,1)
does the week starting Mon ending Sun
(SELECT SUBSTR('5601234',wkndx,1)
does the week starting Tue ending Mon
(SELECT SUBSTR('4560123',wkndx,1)
does the week starting Wed ending Tue
(SELECT SUBSTR('3456012',wkndx,1)
does the week starting Thu ending Wed
(SELECT SUBSTR('2345601',wkndx,1)
does the week starting Fri ending Thu
(SELECT SUBSTR('1234560',wkndx,1)
does the week starting Sat ending Fri
(SELECT SUBSTR('0123456',wkndx,1)
does the week starting Sun ending Sat
You need to use the DAYOFWEEK function
If the End of the Week in Saturday, any date can be morphed into a Saturday.
For example, this computes the upcoming Saturday
SELECT DATE(NOW() + INTERVAL (7 - DAYOFWEEK(NOW())) DAY);
This computes the upcoming Sunday (you must use WEEKDAY function instead)
SELECT DATE(NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY);
For a table called mytable with a Date Column called theDate, your summation would be
Saturday
SELECT COUNT(1) DateCount,EndOfWeekDate FROM
(
SELECT DATE(theDate + INTERVAL (7 - DAYOFWEEK(theDate)) DAY) EndOfWeekDate
FROM mytable
) A GROUP BY EndOfWeekDate;
Sunday
SELECT COUNT(1) DateCount,EndOfWeekDate FROM
(
SELECT DATE(theDate + INTERVAL (6 - WEEKDAY(theDate)) DAY) EndOfWeekDate
FROM mytable
) A GROUP BY EndOfWeekDate;
Give it a Try !!!
CAVEAT if you want any other end of week other than Saturday and Sunday, I wrote an insane algorithm back in Sep 22, 2011 for computing any week starting and ending whatever day.
Best Answer
I hope to understand your question. You want translate a date to a customizable first day of week. I call it start_day_of_week. So if your start_day_of_week is greater than day_of_weeek you must subtract the difference otherwise you must first subtract seven day (i.e. translate to previous week) and then add the difference. For example:
I think it better to write a mysql function that takes as parameters a day, a customizable start_day_of_week and returns a date translated.
To begin, I write some test. I use a table with three columns. One for input date, and input start_day_of_week, and a column with the result date. So the date '2014-10-23' translates to date '2014-10-19' when the start_day_of_week is Sunday (1), but it translates to '2014-10-17' when the start day is Friday (7).
The function is:
So to test the function I write a simple query:
So to respond you answer