MySQL COUNT on Tomorrow's Date based on CURDATE - mysql

I have a MYSQL COUNT Status that is working great by counting records greater than CURDATE. How do I specify this query to ONLY count tomorrow based on CURDATE for the column specified?
SELECT COUNT(ID) as total_count
FROM HTG_ScheduleRequest
WHERE (ScheduleDateCurrent > CURDATE())
AND JobStatus = '3'
GROUP BY SSR

The "easy" way is:
WHERE date(ScheduleDateCurrent) = date_add(CURDATE(), interval 1 day) AND JobStatus = '3'
The better way is:
WHERE JobStatus = '3' AND
ScheduleDateCurrent >= date_add(CURDATE(), interval 1 day) AND
ScheduleDateCurrent < date_add(CURDATE(), interval 2 day)
The reason this is better is because it can take advantage of an index on JobStatus, ScheduleDateCurrent, if one is available.

The following query would do the trick
SELECT COUNT(ID) as total_count
FROM HTG_ScheduleRequest
WHERE (ScheduleDateCurrent BETWEEN DATE_ADD(DATE(CURDATE()), INTERVAL 1 DAY)
AND DATE_ADD(DATE(CURDATE()), INTERVAL 2 DAY))
AND JobStatus = '3'
GROUP BY SSR

To count only the rows having tomorrow's date (and non-NULL ID), you could do this:
SELECT COUNT(ID) as total_count
FROM HTG_ScheduleRequest
WHERE (ScheduleDateCurrent = ADDDATE(CURDATE(),1))
AND JobStatus = '3'
GROUP BY SSR

Related

SQL condition depending on result in same query

I have this mySQL statement:
SELECT (SELECT COUNT(id) from online_users WHERE name='lol') as online, id, name, city
FROM players
WHERE name='lol'
AND last_action < date_sub(now(), interval 1 hour)
however I want the last_action interval to vary; 1 minute if online is 1, and 1 hour if online is 0
Can this be done all in one query? How so?
SELECT (SELECT COUNT(id) from online_users WHERE name='lol') as online, id, name, city
FROM players
WHERE name='lol'
AND last_action < date_sub(now(), interval 1 CASE WHEN online=0 THEN hour ELSE MINUTE END)
Try above code.
Hope this will help.
You could use an OR condition, like this:
SELECT (SELECT COUNT(id) from online_users WHERE name='lol') as online, id, name, city
FROM players
WHERE name='lol'
AND ((last_action < date_sub(now(), interval 1 hour) AND online = 1) OR (last_action < date_sub(now(), interval 1 minute) AND online = 0))

How do I subtract two results from a SELECT statement within that statement?

This pulls back two int values of yesterday and today. I'd like to subtract the two results from within the statement in a third column called difference:
SELECT (
SELECT COUNT(*)
FROM collectors_users
WHERE DATE(dateadded) = CURDATE() - INTERVAL 1 DAY
) AS yesterday, COUNT(*) AS today
FROM collectors_users
WHERE DATE(dateadded) = CURDATE()
You need to repeat the expressions. SQL (in general) does not allow you to re-use column aliases in the same SELECT. You can simplify the logic to:
SELECT SUM(DATE(dateadded) = CURDATE() - INTERVAL 1 DAY) AS yesterday,
SUM(DATE(dateadded) = CURDATE()) as today,
(SUM(DATE(dateadded) = CURDATE()) -
SUM(DATE(dateadded) = CURDATE() - INTERVAL 1 DAY)
) as diff
FROM collectors_users
WHERE dateadded >= CURDATE() - INTERVAL 1 DAY AND
dateadded < CURDATE() + INTERVAL 1 DAY;
Note that the logic for the WHERE clause covers two days. Also, it does not use DATE(). This would allow the query to use an index, if available.

Count of records for the last and current month unix timestamp

I need to make statistics page for billing with auto query. For example now is july and query must show count of record for the june and current count of records on current day(only july). Sort of:
"records for the last month - 85, for the current day - 32"
I have table customer and row create_time but it is in unix timestamp. Tried
SELECT COUNT(*) FROM customer WHERE create_time >=
UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 MONTH))
but its absolutely not what i want.
I would appreciate for any help.
Try this:
SELECT *
FROM ( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01')
AND create_time < LAST_DAY(now() - interval 1 month )
) AS s1,
( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(extract(year from now()),'-',extract(month from now()),'-01')
AND create_time <= now()
) AS s2
You can do this by adding condition to the WHERE:
create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 month))
in PHP you can calculate the one month back date
$newDate = strtotime('-1 month');
and use this in query

MySql SELECT COUNT with two criterias

I'm trying to do a query that fetches data per the last hour and the two last hours. It's just a hits counter.
So I would like to get the resultset as follows:
id, page_url, last_hour_hits, two_last_hours_hits
The table is very simple:
id (autonumber)
page_url
time_stamp
I tried the query below:
SELECT
page_url,
COUNT(page_url) AS last_hour_hits
FROM stats
WHERE time_stamp > '2015-08-01 00:00:00'
GROUP BY page_url
('2015-08-01 00:00:00' is calculated for the last hour)
It works fine, but I have now idea how to add the 'two_last_hours' counter.
Thank you in advance.
With MySQL you can use the fact that boolean expression returns 1 for true and simplify the query to this:
SELECT
page_url
,sum(time_stamp > (now() - interval 1 hour)) as last_hour_hits
,sum(time_stamp > (now() - interval 2 hour)) as two_last_hours_hits
FROM stats
GROUP BY page_url;
This uses now() to get the current time and subtracts the interval as needed.
Just put your WHERE clause with two hours before ('2015-07-31 23:00:00') and a CASE WHEN to count for the last hour ('2015-08-01 00:00:00') :
SELECT
page_url,
COUNT(DISTINCT CASE WHEN time_stamp > '2015-08-01 00:00:00' THEN id ELSE NULL END) as last_hour_hits
COUNT(*) AS two_last_hours_hits
FROM stats
WHERE time_stamp > '2015-07-31 23:00:00'
GROUP BY page_url;
you can use join between two result sets- one for the one hour and the other for the two hours:
SELECT stats.page_url, last_hour_hits, last_two_hours_hits from
stats inner join (
SELECT page_url, COUNT(page_url) AS last_hour_hits
FROM stats WHERE time_stamp > '2015-08-01 00:00:00'
GROUP BY page_url ) as last_hour
on stats.page_url = last_hour.page_url
inner join (
SELECT page_url, COUNT(page_url) AS last_two_hours_hits
FROM stats WHERE time_stamp > '2015-07-31 23:00:00'
GROUP BY page_url ) as last_two_hours
on stats.page_url = last_two_hours.page_url

Combine SUB_DATE AND TIMEDIFF to substract 1 hour in mysql

I'm having problems with timediff and DATE_SUB. Here is my MYSQL query:
SELECT id, clock_user_id, clock_date,
(Select clock_time from aura_clock where aura_clock.clock_type = 'Start' and
aura_clock.clock_date = t1.clock_date) as start, (Select clock_time
from aura_clock where aura_clock.clock_type = 'Stop'
and aura_clock.clock_date =
t1.clock_date) as Stop,
THE PROBLEM START HERE
TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time)) as spent
FROM aura_clock t1 WHERE t1.clock_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL
WEEKDAY(CURDATE()) DAY),INTERVAL 15 day)
AND t1.clock_date < DATE_SUB(curdate(),INTERVAL DAYOFWEEK(curdate()) + 6 day)
GROUP BY clock_date
The result is :
Now, I want to subtract 1 hour from the time spent using DATE_SUB but it didn't work.
as mirkobrankovic wrote:
((TIMEDIFF((select clock_time FROM aura_clock t
WHERE t.clock_date = t1.clock_date AND t.clock_time > t1.clock_time
ORDER BY t.clock_time LIMIT 1), MIN(clock_time))) - INTERVAL 1 HOUR) AS new_spent
should work
EDIT:
The best i got is time in seconds :
http://sqlfiddle.com/#!2/18160/66/0
a lot depends on MYSQL version