Trying to select the number of mysql rows inserted yesterday - mysql

I can normally do this but it appears my brain is not functioning well right now and I'm missing something.
Every day via a cron job that is run at 1am, I want to get a count of rows that were inserted yesterday, and the date.
ie
SELECT DATE(added) as date, COUNT(*) FROM bookings WHERE added = DATE_SUB(NOW(), INTERVAL 1 DAY) GROUP BY date
'added' contains a timestamp ie '2011-04-18 12:31:31'
What am I getting wrong here? I know there are many rows added yesterday but my query is returning 0 results and no mysql_errors :(
Any ideas?

Please try
SELECT DATE(added) as yesterday, COUNT(*) FROM bookings WHERE DATE(added) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY yesterday
or perhaps
SELECT DATE(added) as yesterday, COUNT(*) FROM bookings WHERE DATE(added) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) GROUP BY yesterday
Updated
Corrected the WHERE part.

well whatever NOW() is will return the time portion and unless they were added at exactly that time the day before they wont be counted.
So either use BETWEEN and specify time range, or format the date in your query to only match on the day month year components and not time

WHERE added = does only match exact NOW() - 1 DAY, you should select by a range instead.

Related

TRICKY Rolling Sum Query with Reset

Assuming that there are 6 months of historical data with hundreds of rides per day:
Write a query that returns, for each of the last 90 days, a count of the rides taken in the 7 day window preceding that day
I would like to find a way to write this in MySQL but have had some trouble with having a rolling sum that resets along with how I could cut up timestamps to reflect a day of the year/date and to then group by that.
I have tried writing subqueries that will limit the sum to a week prior and then place an additional limit of 90 days after that but cannot seem to get the code to return any output.
I have tried writing this is PostgreSQL using a sort of "window" functionality but am much more comfortable working in MySQL and would like to be able to solve it that way. I am familiar on how to write limits, group and order among other things but I am having trouble with the rolling sum resetting per week.
Thank you for your help!
First you'll want a numbers table/query. There are some tricky CTE ways to do that but it might be easier for now just to add a table with the numbers 1-90 in 90 rows.
Then use that to generate, for each row, a date range. Sorry if the syntax isn't quite correct, but write a query along the lines:
SELECT num, DATE_ADD(CURRENT_DATE(), INTERVAL -(num+7) DAY) startdate, DATE_ADD(CURRENT_DATE(), INTERVAL -num DAY) enddate FROM numbers
Then you can cross-join that with your rides table grouped on num and counting the rows in the range:
SELECT num, startdate, enddate, SUM(CASE WHEN startdate <= ridedate AND ridedate <=
enddate THEN 1 ELSE 0 END) ridecount
FROM (date range query) dts, rides
GROUP BY dts.num
Hope that helps.
Assuming you have data on each day, a correlated subquery might be simplest approach:
select dt,
(select count(*)
from rides r
where r.ridedate >= d.dte - interval 7 day and
r.ridedate < d.date
) as rolling_7
from (select distinct ridedate as dt
from rides
) dt

mysql request every week since last 6 months

I'm trying to request my database to get numbers of rows where a specific login appears from last 6 months ordered by weeks.
The output has to be something like this :
My request is like this :
SELECT count(*) FROM table1 where (`EndDate` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH) and CURDATE()) AND the_login LIKE 'LOGIN 1';
It shows every rows from last 6 month for now.
Could you complete my request or change it if needed please ?
You can use YEAR() and WEEK() to extract them from the date, and group by them .
Note that I didn't use only WEEK because its not enough, if you'll perform this before June, weeks can be misunderstood.
Also, don't use LIKE for exact match, use them only for partially comparison.
SELECT YEAR(t.endDate) as m_Year, WEEK(t.endDate) as m_Week, count(*)
FROM table1 t
where t.`EndDate` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
and CURDATE())
AND t.the_login = 'LOGIN 1'
GROUP BY m_Year,m_Week

SQL select current week/last week

I'm looking for a way of selecting rows based on:
Current week (S-S)
Previous week (S-S)
The problem I'm having is selecting specifically from Sunday-Sunday.
At the moment I'm using:
SELECT SUM(time)
FROM `time`
WHERE `projectid` = '$pid' && created > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Any help would be great, thanks!
MySQL does have a WEEK function that you can use:
SELECT SUM(time)
FROM `time`
WHERE
`projectid` = '$pid'
AND created > NOW() - INTERVAL 2 WEEK
AND WEEK(created) IN (WEEK(NOW()), WEEK(NOW() - INTERVAL 1 WEEK))
Notes
The first condition (created > NOW() - INTERVAL 2 WEEK) is needed to get all the data of the current and previous weeks first and then restrict for the two weeks you are interested in. Otherwise, if you had enough data, you would get the aggregation of all data of the corresponding weeks of every year in your table. It also has the added benefit of allowing the query to use an index on that field.
You also need to use "WEEK(NOW() - INTERVAL 1 WEEK)" due to the first week of the year. Otherwise, WEEK(NOW()) - 1 would have sufficed

MySQL query for rows where date is in this week

I'm trying to query for all rows where an event has been completed THIS week, where weeks start on Sunday. I've tried this query, but it doesn't seem to work (I've tried both CURDATE() and NOW(), not sure if that matters), I get no rows returned:
SELECT `TodoCompleted`.`day`, `TodoCompleted`.`date` FROM `todo_completed` AS
`TodoCompleted` WHERE `TodoCompleted`.`employee_id` = 'whatever' AND
`TodoCompleted`.`completed` = 0 AND WEEK(`TodoCompleted`.`date`) = 'WEEK(NOW())'
I've also tried this, but it isn't what I'm looking for since it pulls records from the previous week:
AND `TodoCompleted`.`date` BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()
I can't just get them all within an interval of a week, I need just THIS week (Sunday - Saturday).
Any help would be much appreciated!
Use YEARWEEK()
AND YEARWEEK(`TodoCompleted`.`date`) = YEARWEEK(CURDATE())
and remove the quotes around it.

mysql: query to display data from previous day doesnt work when its a new month

When I run the below query on the first on a month it returns no data. It is supposed to display the previous day.
The query works ok normally except when it’s the first on the month, am I doing something wrong which will not allow it to see the previous days data as it’s a previous month?
select COUNT(*) from osticket.ost_ticket where DATE(created) = DATE(NOW())-1
You need to use DATE_SUB :
select COUNT(*)
from osticket.ost_ticket
where DATE(created) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY));