I would like to get the data from the last week. A week is Monday to Sunday.
I made this query :
sql = 'SELECT COUNT(*) ' \
'FROM panelname ' \
'WHERE year(date) = year(now()) ' \
'AND date BETWEEN date_sub(now(),INTERVAL 1 WEEK) AND now() '
cursor.execute(sql)
test = cursor.fetchall()
print(test)
The problem with AND date BETWEEN date_sub(now(),INTERVAL 1 WEEK) AND now() is that it takes the data from the past 7 days. If I am Tuesday of the current week and I run it, I will get the data from last week's Tuesday up until this week's Tuesday, which is not what I want. It should give the data from last Monday down to last Sunday.
I also precise that it should not use the DATEADD function, because I don't have the admin rights to use it.
For example : I run the query today, it doesn't matter which day we are, and I get the last week of data (Monday to Sunday).
Thank you.
You must first get the most recent Monday and use it as a reference point to calculate the 5 previous weeks:
SELECT COUNT(*)
FROM panelname
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) day - INTERVAL 5 week
AND date < CURDATE() - INTERVAL WEEKDAY(CURDATE()) day
UPDATE
To get the aggregated count per each week you need to group by week:
SELECT WEEKOFYEAR(date),COUNT(*)
FROM panelname
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) day - INTERVAL 5 week
AND date < CURDATE() - INTERVAL WEEKDAY(CURDATE()) day
GROUP BY WEEKOFYEAR(date)
Related
I need little help with my MySQL query. I have 2 MySQL queries:
This code gave me all the data from the beginning of the current week to the end of the current week:
//Earnings - this week
$globalSalesThisW = mysqli_query($connection, "SELECT SUM(earnings) AS globalSales91 FROM `affil` WHERE DATE(date + INTERVAL (YEAR(NOW()) - YEAR(date)) YEAR)
BETWEEN DATE(NOW() - INTERVAL WEEKDAY(NOW()) DAY) AND DATE(NOW() + INTERVAL 6 - WEEKDAY(NOW()) DAY); ");
and this code gave me all the data from the beginning of the current month to the end of the current month:
//Earnings - this month
$globalSalesThMonth = mysqli_query($connection, "SELECT SUM(earnings) AS globalSalesThMonth1 FROM `affil` WHERE date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now()) ");
I need the first query (Earnings - this week) to give me the data from the beginning of the current week (monday) until now (today including). And second query (Earnings - this month) to give me the data from the beginning of the current month until now (today including). Could someone help me with this? I am lost :) Thanks.
How do I fetch last week data from monday time (00:00:01) and end on sunday time (23:59:59)...
same as this current week from monday time (00:00:01) and end on sunday time (23:59:59)
WHat I tried!
$query = "SELECT users.name,count(*) as count,
campaign.campaign_name,
campaign.payout_cost*count(*) as totalPrice
FROM users
JOIN transactions on users.uid=transactions.uid
JOIN campaign on campaign.campaign_id_id=transactions.campaign_id
WHERE uid=$uid
AND `date` >= DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())+6 DAY)
AND `date` < DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 DAY)
GROUP BY campaign.campaign_name_name ";
You are on the right track by avoiding functions like week() on the column -- that just messes up the optimizer. On the other hand, the uid parameter should be passed as a parameter rather than munging the query string.
You want to use the weekday() function because you want weeks to start on a Monday. Just some arcaneness of MySQL: weekday() returns 0 for Monday whereas dayofweek() returns 2 for Monday.
So, the logic for the current week would be:
date >= curdate() - interval weekday(curdate()) day and
date < curdate() + interval 7 - weekday(curdate()) day
For last week, this would be:
date >= curdate() - interval 7 + weekday(curdate()) day and
date < curdate() + interval - weekday(curdate()) day
Notes that curdate() (or current_date) returns the current date with no time component, so no date() is required.
Couple of ways to do it...
select data from tableName
where date between date_sub(now(),INTERVAL 1 WEEK) and now();
select data FROM tableName
wherdate >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
You can use WEEK() function, which returns the week number for a given date, by adding
AND WEEK(date-INTERVAL 1 DAY) = WEEK(NOW()) - 1 to get current week's data starting from monday upto sunday,
and
AND WEEK(date-INTERVAL 1 DAY) = WEEK(NOW()) - 2 for the previous week's data
into the WHERE condition after WHERE uid=$uid
such as
$query = "SELECT c.campaign_name,
COUNT(*) as total_count,
SUM(c.payout_cost) as total_payout
FROM transactions t
JOIN campaign c
ON c.campaign_id = t.campaign_id
WHERE uid = $uid
AND WEEK(date - INTERVAL 1 DAY) = WEEK(NOW()) - 1
GROUP BY c.campaign_name ";
and replace WEEK(NOW()) - 1 with WEEK(NOW()) - 2, also
Demo
I want id whose date join is saturday to friday night.
SELECT id
FROM signup
WHERE yearweek(DATE(datejoin), 6) = yearweek(curdate(), 6)
You can use WeekDay function, this function returns an Integer value for every days of week from 0 for Monday to 6 for Sunday, if you subtract WeekDay from current date you will get the date of last Monday, if you want the date of the next Friday you can get it with a query like this :
CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 4 DAY
In you query you want to start your week from Saturday to Friday so result of your query depends on the current day of week, if the current day of week is before Friday(from 0 to 4), you should get the date of next Friday but if current day of the week is after Friday(5 or 6) you should get the date of two Friday next !, the end of your week (Saturday) has this situation too
Look at this query, I hope it's clear enough to you :
SELECT id FROM signup WHERE
datejoin BETWEEN
(CASE
WHEN WEEKDAY(CURDATE()) > 4 THEN
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 5 DAY)
ELSE
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) + 2 DAY)
END)
AND
(CASE
WHEN WEEKDAY(CURDATE()) > 4 THEN
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 11 DAY)
ELSE
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 4 DAY)
END)
Answer:
SELECT id from signup where datejoin between (CURRENT_DATE - INTERVAL 7 DAY) AND CURRENT_DATE
I'm building a weekly report using MySQL queries. First I get week number by
SELECT WEEK(CURDATE());
Then I need to display the starting date of the week (Monday for my region). How to do it with MySQL only?
Thanks!
If you need date of monday in current week
try this:
SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
It return you monday date of current week.
In fact, DATE_ADD is not necessary and complicates the logic. Here is the simplest version for "Date of the first day of the current week":
select date(curdate() - interval weekday(curdate()) day)
which would translate to:
get the date() part of the (current date - an interval of N days)
where N = today's number in the week, i.e. Thu is number 3 (for a week starting on Monday)
Then, getting the Monday of previous week, is:
select date(curdate() - interval weekday(curdate()) day - interval 1 week)
which is more readable IMHO.
For thhose who need the Monday date of the current week.
If you count Monday as the first day of the week:
SELECT STR_TO_DATE(CONCAT(YEARWEEK(NOW(), 1),'Monday'), '%x%v %W');
If you count Sunday as the first day of the week:
SELECT STR_TO_DATE(CONCAT(YEARWEEK(NOW()),'Monday'), '%X%V %W');
SELECT DATE_ADD((SELECT curdate() - INTERVAL (WEEKDAY(curdate())+1)DAY),INTERVAL 1 DAY) as current_monday
-- current week monday
(SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY))
-- current week friday
SELECT DATE_ADD((SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)),INTERVAL 4 DAY);
-- next week monday
SELECT date(curdate() - interval weekday(curdate()) day + interval 1 week);
-- next week friday
SELECT DATE_ADD((SELECT date(curdate() - interval weekday(curdate()) day + interval 1 week)),INTERVAL 4 DAY);
In fact, we substruct days, not add them. Just more simple expression:
DATE_SUB( CURDATE(), INTERVAL WEEKDAY( CURDATE() )
You can use this:
<?php
$week = date('W');
$year = date('Y');
echo $date1 = date(
'Y-m-d',
strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT))
);
?>
How do I SELECT the first week of a previous month I've tried
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>=(CURDATE()- 1 WEEK - INTERVAL 2 week)";
This was used to try and select the third week but this didn't work
Does this work for you:
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>= curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week
AND startDate < curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week _+ interval 1 week";
?
The idea here is that we first go back a month, then find the start of the week (assuming Monday, for Sunday we will need some extra tweaking), then figure out how many whole weeks it has been from the start of the month, and subtract that many weeks from the date so far. This takes us back to the start of the first week of the month. For the end of the range we just add one week to the start.
Ah, your question became more detailed.
Not really familiar with sql, there might be better but something like:
SELECT repairId , startDate,catId, statusId FROM repair
WHERE EXTRACT(YEAR_MONTH FROM start_date) = EXTRACT(YEAR_MONTH FROM NOW()) - 1 AND CAST(EXTRACT(DAY FROM start_date) / 7 + 1 as INT) = ?;
Basically, extract the year month components to compare year and month and then extract the day of month use the flooring caused by integer truncation to get the week to compare with whatever week you are looking for
mysql return rows matching year month