group by week, but new group if it falls into next month - mysql

I am having a hard time wrapping my head around how I would do this.
I have daily (most days) invoice data that I need to group in buckets of weeks. However if the week goes into the next month I need the bucket to only have the amount of days in it that fall in the current month, and then the next bucket would start on the 1st - the next saturday. so that the next full week starts again on sunday.
Right now we just don't group it at all, and just export by day which gives us ~60 million rows for the rolling 2 years (it is more complex than the example as it also is split by item and customer). This then gets imported into our demand planning software which has both a weekly and monthly model. It has no problem dumping them into the correct buckets when it is by day.
However I would like to decrease this ~60 million rows as we are running into some time constraints. But it still has to accurately work with both the weekly and monthly models the data gets imported into.
How can I group this way?
Example Data set
+------------+------------+
| date | sales |
+------------+------------+
| 2014-06-22 | 100 |
| 2014-06-23 | 200 |
| 2014-06-24 | 300 |
| 2014-06-25 | 150 |
| 2014-06-26 | 170 |
| 2014-06-27 | 210 |
| 2014-06-28 | 220 |
| 2014-06-29 | 120 |
| 2014-06-30 | 110 |
| 2014-07-01 | 190 |
| 2014-07-02 | 210 |
| 2014-07-03 | 100 |
| 2014-07-04 | 140 |
| 2014-07-05 | 150 |
| 2014-07-06 | 130 |
| 2014-07-07 | 420 |
| 2014-07-08 | 310 |
| 2014-07-09 | 290 |
| 2014-07-10 | 180 |
| 2014-07-11 | 140 |
| 2014-07-12 | 210 |
+------------+------------+
Expected Result:
+------------+------------+
| date | sum(sales) |
+------------+------------+
| 2014-06-22 | 1350 | 7 days in group
| 2014-06-29 | 230 | 2 days in group
| 2014-07-01 | 790 | 5 days in group
| 2014-07-06 | 1680 | 7 days in group
+------------+------------+
EDIT:
We came up with a working solution. Feel free to improve on it if wanted, or not.
SELECT DATE(IF(
MONTH(DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY)) = MONTH(`date`)
, DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY)
, DATE_FORMAT(`date`,'%Y-%m-01')
)) AS datekey
, SUM(val) AS valsum
FROM tmp.testdata
GROUP BY IF(
MONTH(DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY)) = MONTH(`date`) -- If the closest previous Sunday from date falls within the same month as the date...
, DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY) -- ...use the date of the closest previous Sunday as the key...
, DATE_FORMAT(`date`,'%Y-%m-01') -- ...otherwise use the 1st of the month the date falls in as the key (since that must mean the date falls in that opening partial week).
)
ORDER BY datekey
Thanks all! We combined some of this together and ended up with:
SELECT MIN(`date`) AS datekey
, SUM(val) AS valsum
FROM tmp.testdata
GROUP BY DATE_FORMAT(`date`, '%U'), MONTH(`date`), YEAR(`date`)
ORDER BY datekey
OR in the case we ALWAYS want the bucket to be sunday or the 1st (for instance when not all days have invoices) we combined my solution with the one here, since the group here was faster
SELECT
DATE(IF(MONTH(DATE_SUB(`date`,
INTERVAL DAYOFWEEK(`date`) - 1 DAY)) = MONTH(`date`),
DATE_SUB(`date`,
INTERVAL DAYOFWEEK(`date`) - 1 DAY),
DATE_FORMAT(`date`, '%Y-%m-01'))) AS datekey,
SUM(val) AS valsum
FROM
tmp.testdata
GROUP BY DATE_FORMAT(`date`, '%U') , MONTH(`date`) , YEAR(`date`)
ORDER BY datekey

Here's something to think about...
calendar is a simple table of dates...
SELECT MIN(dt),YEARWEEK(dt),MONTH(dt) FROM calendar WHERE dt BETWEEN '2014-01-01' AND '2014-12-31' GROUP BY YEARWEEK(dt),MONTH(dt);
+------------+--------------+-----------+
| MIN(dt) | YEARWEEK(dt) | MONTH(dt) |
+------------+--------------+-----------+
| 2014-01-01 | 201352 | 1 |
| 2014-01-05 | 201401 | 1 |
| 2014-01-12 | 201402 | 1 |
| 2014-01-19 | 201403 | 1 |
| 2014-01-26 | 201404 | 1 |<-- Overlap
| 2014-02-01 | 201404 | 2 |<-- Overlap
| 2014-02-02 | 201405 | 2 |
| 2014-02-09 | 201406 | 2 |
| 2014-02-16 | 201407 | 2 |
| 2014-02-23 | 201408 | 2 |<-- Overlap
| 2014-03-01 | 201408 | 3 |<-- Overlap
| 2014-03-02 | 201409 | 3 |
| 2014-03-09 | 201410 | 3 |
| 2014-03-16 | 201411 | 3 |
| 2014-03-23 | 201412 | 3 |
| 2014-03-30 | 201413 | 3 |<-- Overlap
| 2014-04-01 | 201413 | 4 |<-- Overlap
| 2014-04-06 | 201414 | 4 |
| 2014-04-13 | 201415 | 4 |
| 2014-04-20 | 201416 | 4 |
| 2014-04-27 | 201417 | 4 |<-- Overlap
| 2014-05-01 | 201417 | 5 |<-- Overlap
| 2014-05-04 | 201418 | 5 |
| 2014-05-11 | 201419 | 5 |
| 2014-05-18 | 201420 | 5 |
| 2014-05-25 | 201421 | 5 |<-- No overlap
| 2014-06-01 | 201422 | 6 |<-- No overlap
| 2014-06-08 | 201423 | 6 |
| 2014-06-15 | 201424 | 6 |
| 2014-06-22 | 201425 | 6 |
| 2014-06-29 | 201426 | 6 |<-- Overlap
| 2014-07-01 | 201426 | 7 |<-- Overlap
| 2014-07-06 | 201427 | 7 |
| 2014-07-13 | 201428 | 7 |
| 2014-07-20 | 201429 | 7 |
| 2014-07-27 | 201430 | 7 |<-- Overlap
| 2014-08-01 | 201430 | 8 |<-- Overlap
| 2014-08-03 | 201431 | 8 |
| 2014-08-10 | 201432 | 8 |
| 2014-08-17 | 201433 | 8 |
| 2014-08-24 | 201434 | 8 |
| 2014-08-31 | 201435 | 8 |<-- Overlap
| 2014-09-01 | 201435 | 9 |<-- Overlap
| 2014-09-07 | 201436 | 9 |
| 2014-09-14 | 201437 | 9 |
| 2014-09-21 | 201438 | 9 |
| 2014-09-28 | 201439 | 9 |<-- Overlap
| 2014-10-01 | 201439 | 10 |<-- Overlap
| 2014-10-05 | 201440 | 10 |
| 2014-10-12 | 201441 | 10 |
| 2014-10-19 | 201442 | 10 |
| 2014-10-26 | 201443 | 10 |<-- Overlap
| 2014-11-01 | 201443 | 11 |<-- Overlap
| 2014-11-02 | 201444 | 11 |
| 2014-11-09 | 201445 | 11 |
| 2014-11-16 | 201446 | 11 |
| 2014-11-23 | 201447 | 11 |
| 2014-11-30 | 201448 | 11 |<-- Overlap
| 2014-12-01 | 201448 | 12 |<-- Overlap
| 2014-12-07 | 201449 | 12 |
| 2014-12-14 | 201450 | 12 |
| 2014-12-21 | 201451 | 12 |
| 2014-12-28 | 201452 | 12 |
+------------+--------------+-----------+

SELECT min(date),sum(sales) FROM sales GROUP BY WEEKOFYEAR(date), MONTH(date);
Update: WEEKOFYEAR() will use the MySQL calendar which starts the week on a Monday. So I found you can use DATE_FORMAT to get the week number starting with Sunday.
SELECT min(date),sum(sales) FROM sales GROUP BY DATE_FORMAT(date, '%U'), MONTH(date);

We came up with a working solution.
SELECT DATE(IF(
MONTH(DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY)) = MONTH(`date`)
, DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY)
, DATE_FORMAT(`date`,'%Y-%m-01')
)) AS datekey
, SUM(val) AS valsum
FROM tmp.testdata
GROUP BY IF(
MONTH(DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY)) = MONTH(`date`) -- If the closest previous Sunday from date falls within the same month as the date...
, DATE_SUB(`date`, INTERVAL DAYOFWEEK(`date`) - 1 DAY) -- ...use the date of the closest previous Sunday as the key...
, DATE_FORMAT(`date`,'%Y-%m-01') -- ...otherwise use the 1st of the month the date falls in as the key (since that must mean the date falls in that opening partial week).
)
ORDER BY datekey

Related

MYSQL - Get all months between two dates (from, to) and data for this months

I have table something like that:
| id | date | user_id | value |
---------------------------------------------
| 1 | 2019-01-10 | 3 | 20
| 2 | 2019-04-08 | 3 | 30
| 3 | 2019-06-04 | 3 | 40
| 4 | 2019-08-20 | 3 | 50
| 5 | 2019-11-19 | 3 | 60
| 6 | 2019-01-11 | 4 | 70
| 7 | 2019-02-20 | 4 | 11
| 8 | 2019-03-11 | 4 | 12
| 9 | 2019-07-12 | 4 | 23
--------------------------------
and I want to get values between two dates: date_from and date_to. And all months from this interval.
For example:
date_from = 2019-01-08;
date_to = 2019-09-10;
So for user_id = 3 i want to get something like that:
| date | value
-------------------------
| 2019-01 | 20 |
| 2019-02 | NULL |
| 2019-03 | NULL |
| 2019-04 | 30 |
| 2019-05 | NULL |
| 2019-06 | 40 |
| 2019-07 | NULL |
| 2019-08 | 50 |
| 2019-09 | NULL |
--------------------------
Is anyone help me? Thanks!
You can use a recursive CTE to generate the dates and then left join:
with recursive dates as (
select date('2019-01-08') as dte
union all
select dte + interval 1 day
from dates
where dte < '2019-09-10'
)
select extract(year_month from d.dte) as yyyymm, sum(t.value)
from dates d left join
t
on d.dte = t.date and t.user_id = 3
group by yyyymm;
Here is a db<>fiddle.

Show counts of all date according to another table in sql?

I have two tables, One is calendar and second is final_registration as follow:
My Calendar table:
*--------------------*
| S.No. | datefield |
*--------------------*
| 1 | 2019-01-01 |
| 2 | 2019-01-02 |
| 3 | 2019-01-03 |
| 4 | 2019-01-04 |
| 5 | 2019-01-05 |
| 6 | 2019-01-06 |
| 7 | 2019-01-07 |
| 8 | 2019-01-08 |
| 9 | 2019-01-09 |
| 10 | 2019-01-10 |
| 11 | 2019-01-11 |
| 12 | 2019-01-12 |
| 13 | 2019-01-13 |
| 14 | 2019-01-14 |
| 15 | 2019-01-15 |
| 16 | 2019-01-16 |
| 17 | 2019-01-17 |
| 18 | 2019-01-18 |
| 19 | 2019-01-19 |
| 20 | 2019-01-20 |
| 21 | 2019-01-21 |
| 22 | 2019-01-22 |
| 23 | 2019-01-23 |
| 24 | 2019-01-24 |
| 25 | 2019-01-25 |
| 26 | 2019-01-26 |
| 27 | 2019-01-27 |
| 28 | 2019-01-28 |
| 29 | 2019-01-29 |
| 30 | 2019-01-30 |
| 31 | 2019-01-31 |
---------------------
My Second table:
*-----------------------------------------*
| id | event_id | name | booking_date |
*-----------------------------------------*
| 1 | 101 | Ritu | 2019-01-15 13:21 |
| 2 | 101 | Seeta | 2019-01-15 18:21 |
| 3 | 101 | Geeta | 2019-01-16 13:21 |
| 4 | 102 | Wasim | 2019-01-16 14:21 |
| 5 | 102 | Rahul | 2019-01-17 13:21 |
| 6 | 101 | Gagan | 2019-01-17 14:21 |
| 7 | 101 | Sunny | 2019-01-17 15:21 |
| 8 | 101 | Aman | 2019-01-17 16:21 |
-------------------------------------------
I am trying below output:
*--------------------*
| datefield | count |
*--------------------*
| 2019-01-01 | 0 |
| 2019-01-02 | 0 |
| 2019-01-03 | 0 |
| 2019-01-04 | 0 |
| 2019-01-05 | 0 |
| 2019-01-06 | 0 |
| 2019-01-07 | 0 |
| 2019-01-08 | 0 |
| 2019-01-09 | 0 |
| 2019-01-10 | 0 |
| 2019-01-11 | 0 |
| 2019-01-12 | 0 |
| 2019-01-13 | 0 |
| 2019-01-14 | 0 |
| 2019-01-15 | 2 |
| 2019-01-16 | 1 |
| 2019-01-17 | 3 |
---------------------
So as today 17-Jan so i need output till current date. I have tried query but it is not given me 01-Jan to 14-Jan
SELECT calendar.datefield, COUNT(calendar.datefield)
FROM calendar
LEFT JOIN final_registration
ON DATE_FORMAT(calendar.datefield, '%Y-%m-%d') = DATE_FORMAT(final_registration.booking_date, '%Y-%m-%d')
WHERE DATE_FORMAT(calendar.datefield, '%Y-%m-%d') <= DATE_FORMAT( CURDATE(), '%Y-%m-%d' )
AND DATE_FORMAT(calendar.datefield, '%Y-%m') = DATE_FORMAT( CURDATE(), '%Y-%m' )
AND final_registration.event_id = '101'
GROUP BY DATE_FORMAT(calendar.datefield, '%Y-%m-%d')
My query gave me below result:
*--------------------*
| datefield | count |
*--------------------*
| 2019-01-15 | 2 |
| 2019-01-16 | 1 |
| 2019-01-17 | 3 |
*--------------------*
I have tried lots but not able to get my result.
You are making your LEFT join into an INNER join by including criteria in the WHERE clause. Try this:
SELECT calendar.datefield, COUNT(calendar.datefield)
FROM calendar
LEFT JOIN final_registration
ON DATE_FORMAT(calendar.datefield, '%Y-%m-%d') = DATE_FORMAT
(final_registration.booking_date, '%Y-%m-%d')
AND final_registration.event_id = '101'
WHERE DATE_FORMAT(calendar.datefield, '%Y-%m-%d') <= DATE_FORMAT( CURDATE(), '%Y-%m-%d' )
AND DATE_FORMAT(calendar.datefield, '%Y-%m') = DATE_FORMAT( CURDATE(), '%Y-%m' )
GROUP BY DATE_FORMAT(calendar.datefield, '%Y-%m-%d')
You need to move some of the restrictions in the WHERE clause to the ON clause:
SELECT
c.datefield,
COUNT(f.booking_date) AS cnt
FROM calendar c
LEFT JOIN final_registration f
ON c.datefield = DATE(f.booking_date) AND
f.event_id = '101'
WHERE
c.datefield BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND CURDATE()
GROUP BY
c.datefield;
Demo
Note that you want to count a field from the final_registration table, which appears on the right side of the left join.
The delay in my answer was due to cleaning up your query to remove all the unneeded calls to DATE_FORMAT. You should be able to deal directly with the datefield. As for the WHERE clause, it seems that you just want dates which are in the current month, but no later than the current day in the month.

sql joins with multiple conditions

i have two tables, (say bill and soldproduct)
select * from bill;
+------+------------+------------+
| id | solddate | customerId |
+------+------------+------------+
| 11 | 2018-07-23 | 1 |
| 12 | 2018-07-21 | 1 |
| 13 | 2018-08-02 | 2 |
| 14 | 2018-08-08 | 2 |
| 15 | 2018-08-08 | 1 |
| 16 | 2018-08-08 | 1 |
+------+------------+------------+
select * from soldproduct;
+--------+-------------+----------+-------+------------+
| billid | productname | quantity | price | totalprice |
+--------+-------------+----------+-------+------------+
| 11 | book | 2 | 100 | 200 |
| 11 | pen | 10 | 10 | 100 |
| 11 | pencil | 5 | 2 | 10 |
| 12 | pencil | 5 | 2 | 10 |
| 13 | pen | 10 | 10 | 100 |
| 13 | book | 2 | 100 | 200 |
| 14 | pen | 1 | 10 | 10 |
| 14 | bottle | 1 | 75 | 75 |
| 15 | phone | 1 | 5000 | 5000 |
| 16 | lock | 15 | 50 | 750 |
+--------+-------------+----------+-------+------------+
I need to find the highest bill id using totalprice.
I tried using
select billid,sum(totalprice)
from soldproduct
where billid in (select id from bill where solddate >= date_sub(curdate(),interval 1 month))
group by billid
order by totalprice desc;
and my output is
+--------+-----------------+
| billid | sum(totalprice) |
+--------+-----------------+
| 15 | 5000 |
| 16 | 750 |
| 11 | 310 |
| 13 | 300 |
| 12 | 10 |
| 14 | 85 |
+--------+-----------------+
How do i get the same output with a single query using joins (without using subquery)?
try the following join
select billid,sum(totalprice)
from soldproduct
join bill on soldproduct.billid = bill.id and solddate >= date_sub(curdate(),interval 1
month)
group by billid
order by totalprice desc;
Can you try the below query:(I do not tested it out)
SELECT billid, SUM(totalprice)
FROM soldproduct SP
JOIN bill B ON (B.id = SP.billid)
WHERE B.solddate BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE()
GROUP BY SP.billid
ORDER BY SP.totalprice DESC;

SQL Query: find lots that belong to current auction

I have a table auctions and a table lots:
mysql> select id, auction_name, auction_startdate, auction_planned_closedate from auctions;
+----+--------------+---------------------+---------------------------+
| id | auction_name | auction_startdate | auction_planned_closedate |
+----+--------------+---------------------+---------------------------+
| 1 | Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 |
| 2 | Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00 |
| 3 | Auction 3 | 2016-07-01 00:00:00 | 2016-08-30 00:00:00 |
| 4 | Auction 4 | 2016-09-01 00:00:00 | 2016-10-30 00:00:00 |
+----+--------------+---------------------+---------------------------+
mysql> select id, auction_id, lot_name from lots;
+----+------------+----------+
| id | auction_id | lot_name |
+----+------------+----------+
| 1 | 1 | Lot 1 |
| 2 | 1 | Lot 2 |
| 3 | 1 | Lot 3 |
| 4 | 1 | Lot 4 |
| 5 | 1 | Lot 5 |
| 6 | 1 | Lot 6 |
| 7 | 1 | Lot 7 |
| 8 | 2 | Lot 8 |
| 9 | 2 | Lot 9 |
| 10 | 2 | Lot 10 |
| 11 | 3 | Lot 11 |
| 12 | 3 | Lot 12 |
| 13 | 3 | Lot 13 |
| 14 | 3 | Lot 14 |
| 15 | 4 | Lot 15 |
| 16 | 4 | Lot 16 |
+----+------------+----------+
I want to display only the lots for current auctions (which are auctions 1 and 2 in the example), in other words for which the current time is between the 'auction_startdate' and 'auction_planned_closedate'.
So here is what I want to achieve:
+--------------+---------------------+---------------------------+---------+
| auction_name | auction_startdate | auction_planned_closedate | lots_id |
+--------------+---------------------+---------------------------+---------+
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 1 |
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 2 |
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 3 |
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 4 |
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 5 |
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 6 |
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 | 7 |
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00 | 8 |
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00 | 9 |
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00 | 10 |
+--------------+---------------------+---------------------------+---------+
The following query gets me the current auctions:
mysql> select auction_name, auction_startdate, auction_planned_closedate from auctions where now() >= auction_startdate and now() <= auction_planned_closedate;
+--------------+---------------------+---------------------------+
| auction_name | auction_startdate | auction_planned_closedate |
+--------------+---------------------+---------------------------+
| Auction 1 | 2016-05-01 00:00:00 | 2016-06-30 00:00:00 |
| Auction 2 | 2016-06-01 00:00:00 | 2016-07-30 00:00:00 |
+--------------+---------------------+---------------------------+
and then I do an inner join with the 'lots' table:
select auction_name, auction_startdate, auction_planned_closedate, lots.id
from auctions
where now() >= auction_startdate
and now() <= auction_planned_closedate
inner join lots on auctions.id = lots.auction_id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'inner join lots on auctions.id=lots.auction_id' at
line 1
I'm getting a syntax error which I'm staring blind at for a while.
Wrong order, put the WHERE clause after the JOIN:
select auction_name, auction_startdate, auction_planned_closedate, lots.id
from auctions
inner join lots on auctions.id = lots.auction_id
where now() >= auction_startdate
and now() <= auction_planned_closedate
Your join is in the wrong order:
select a.auction_name, a.auction_startdate, a.auction_planned_closedate, l.id
from auctions a inner join
lots l
on a.id = l.auction_id
where now() >= a.auction_startdate and now() <= a.auction_planned_closedate ;
Notes:
where goes after the from clause. join is not a separate clause; it is an operator in the from clause.
Table aliases make a query easier to write and to read.
Qualify all the column names. This makes it clear where the columns are coming from.

Group consecutive days in Week in MySql

I want to be able to group a table of sales by week but only by the first x number of days.
I can group by week easily
SELECT SUM( order_total_price ) AS total, WEEK(order_time,1) AS week_number
FROM orders
WHERE YEAR( order_time ) = 2014
GROUP BY WEEK( order_time, 1 )
So I want to get the aggregate sum of orders for each day of the week.
I will need to run this query 7 times for each of the days.
Here is some sample data. I have selected a range of totals from Mon-Sun
+-------------+-------------------+
| order_time | order_total_price |
+-------------+-------------------+
| 2014-03-03 | 20 |
| 2014-03-04 | 25 |
| 2014-03-05 | 30 |
| 2014-03-06 | 15 |
| 2014-03-07 | 20 |
| 2014-03-08 | 15 |
| 2014-03-09 | 30 |
| 2014-03-10 | 20 |
| 2014-03-11 | 15 |
| 2014-03-12 | 10 |
| 2014-03-13 | 25 |
| 2014-03-14 | 30 |
| 2014-03-15 | 25 |
| 2014-03-16 | 10 |
+-------------+-------------------+
Here is the results that I am after
+----------+-------------+-------+
| end_day | week_number | total |
+----------+-------------+-------+
| 1 | 10 | 20 |
| 2 | 10 | 45 |
| 3 | 10 | 75 |
| 4 | 10 | 90 |
| 5 | 10 | 110 |
| 6 | 10 | 125 |
| 7 | 10 | 155 |
| 1 | 11 | 20 |
| 2 | 11 | 35 |
| 3 | 11 | 45 |
| 4 | 11 | 70 |
| 5 | 11 | 100 |
| 6 | 11 | 125 |
| 7 | 11 | 135 |
+----------+-------------+-------+
The end_day(1=Mon - 7=Sun) would be the day which the aggregate of the week total is calculated to. Notice how the totals are the aggregate total to that day of the week.
SELECT
sum(order_total_price) AS total,
WEEK(order_time,1) AS week_number, date_format(order_time,'%w') as day_of_week
FROM orders
WHERE YEAR(order_time) = 2014
AND date_format(order_time,'%w') >= 1
AND date_format(order_time,'%w') <= 2
-- date_format(date,format)
-- %w: Day of the week (0=Sunday, 6=Saturday)
GROUP BY WEEK(order_time,1), date_format(order_time, '%w')