I have a table records with start time,end time, duration.
Assume I have three records A,B,C where:
A record has start time at 2019-09-10 00:12:00 and its end time at 2019-09-10 02:30:44.
B record has start time at 2019-09-10 17:34:00 and end time at 2019-09-10 19:34:00
C record had start time at 2019-09-10 22:34:00 and end time at 2019-09-11 02:30:30
Now I have to frame a query where a,b,c records to be pulled within the start time has 2019-09-10 00:00:00 and end time as 2019-09-10 23:59:59 and C record should also be counted till 23:59:59 with a duration.
I have tried but the C record is not displaying it because the end time is different.
Query:
Select
start_time,
end_time,
duration as timespent
from
tablename
where
userid IN(91) AND
'start_time' >='2019-09-10 00:00:00' AND
'end_time' <='2019-09-10 23:59:59'
Can anyone help me how can i frame it.
I am using php codeigniter has a frame work.
Look for sessions witch started or ended on that day :
SELECT start_time, end_time, duration AS timespent
FROM tablename
WHERE DATE(start_time) = '2019-09-10' OR DATE(end_time) = '2019-09-10'
SELECT id, start_time, CASE WHEN end_time > #end_time
THEN #end_time
ELSE end_time
END as end_time
FROM YourTable
WHERE start_time <= #end_time
AND end_time >= #start_time
If you dont know use variables try this first:
SELECT id, start_time, CASE WHEN end_time > '2019-09-10 23:59:59'
THEN '2019-09-10 23:59:59'
ELSE end_time
END as end_time
FROM YourTable
WHERE start_time <= '2019-09-10 23:59:59'
AND end_time >= '2019-09-10 00:00:00'
The duration is the difference between end_time and start_time. But start_time must be 00:00:00 if it's yesterday and end_time 23:59:59 if it's tomorrow. Right ?
Use LEAST() AND GREATEST() to do something like this :
SELECT start_time, end_time, LEAST('2019-09-10 23:59:59', end_time) - GREATEST('2019-09-10 00:00:00', start_time) AS timespent
FROM tablename
WHERE DATE(start_time) = '2019-09-10' OR DATE(end_time) = '2019-09-10'
timespent will be a numeric, not an int.
Related
I need to output data from a DB table that selects records between 3 (not 2) date/time ranges
E.g. start time : 2019-09-07 18.00
end time : 2019-09-07 20.00
so the user should be able to see the record 25 minutes before the start date-time (6.p.m - 18.00), during the event but not after the end date-time (8.p.m -20.00).
I've tried
db->query = "SELECT o_id, schedule, date, start_time, end_time FROM working_schedule WHERE o_id = '".$user_id."'
AND (start_time <= '".date('Y-m-d\TH:i:s', strtotime("-25 minutes"))."' AND start_time >= '".date('Y-m-d\TH:i:s')."')
AND end_time >= '".date('Y-m-d\TH:i:s')."'";
but the result is NULL.
For reference HERE'S a sql fiddle.
Thanks in advance for pointing me in the right direction.
Do you need this ?
select * from working_schedule
where
NOW() BETWEEN DATE_SUB(start_time,INTERVAL 25 MINUTE) AND end_time
I have a table with 3 columns: id, updated_at, click_sum.
Many rows have the exact same updated_at value which makes it hard to simply retrieve the data, order by updated_at and display the sums in a chart.
Since there are multiple sums for the same dates which screws the chart.
What I try to achieve is to get the following output:
update_at | click_sum
-----------+-----------
date1 | 100
date2 | 3
date3 | 235
date4 | 231
Optionally only those dates which are form the last month, week or day AND not simply the dates which are NOW() - 1 month.
The current query I build is very large and doesn't work that well.
It groups by dates (no duplicated dates appear) and SUM()s the clicks correctly but defining from when (last month, week, day) the dates are doesn't seem to work properly.
Query: ($interval stands for MONTH or DAY or SECOND or WEEK)
SELECT d.updated_at, SUM(d.clicks_sum) AS click_sum
FROM aggregated_clicks d
JOIN
(
SELECT c.id, MAX(StartOfChains.updated_at) AS ChainStartTime
FROM aggregated_clicks c
JOIN
(
SELECT DISTINCT a.updated_at
FROM aggregated_clicks a
LEFT JOIN aggregated_clicks b ON (b.updated_at >= a.updated_at - INTERVAL 1 DAY AND b.updated_at < a.updated_at)
WHERE b.updated_at IS NULL
) StartOfChains ON c.updated_at >= StartOfChains.updated_at
GROUP BY c.id
) GroupingQuery
ON d.id = GroupingQuery.id
WHERE GroupingQuery.ChainStartTime >= DATE_SUB(NOW(), INTERVAL 1 $interval)
GROUP BY GroupingQuery.ChainStartTime
ORDER BY GroupingQuery.ChainStartTime ASC
maybe I'm assuming too much about the nature of your question (and the table it refers to), but I think this can be done much more simply than the query you've shown.
figuring the latest completed month isn't very hard.
it starts with knowing the first date of this current month -- use this:
date_sub(curdate(), interval (extract(day from curdate())-1) day)
and to know the first day of that previous month, use this:
date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
so if you want to get the sums for just the days in between -- i.e. the latest completed month, use this:
select updated_at, sum(click_sum) from aggregated_clicks
where updated_at >= date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
and updated_at < date_sub(curdate(), interval (extract(day from curdate())-1) day)
group by updated_at;
figuring the lastest completed week is just as easy. this example will assume a Sunday-Saturday week.
because of the way the ODBC standard defines date numbers, it's easy to find the end (Saturday) of the previous week:
date_sub(curdate(), interval dayofweek(curdate()) day)
and the beginning (Sunday) of that week is six days before that:
date_sub(curdate(), interval (dayofweek(curdate())+6) day)
so if you want to get the sums for just the days in between -- i.e. the latest completed week, use this:
select updated_at, sum(click_sum) from aggregated_clicks
where updated_at >= date_sub(curdate(), interval (dayofweek(curdate())+6) day)
and updated_at <= date_sub(curdate(), interval dayofweek(curdate()) day)
group by updated_at;
and of course figuring based on the latest completed day is super easy.
to get the date of the previous day, use this:
date_sub(curdate(), interval 1 day)
so if you want the sums just for yesterday, use this:
select updated_at, sum(click_sum) from aggregated_clicks
where updated_at = date_sub(curdate(), interval 1 day)
group by updated_at;
NOTE: I've tested these queries using MySQL 5.1, YMMV.
----------
UPDATE: since the date column is a datetime, simply change all references to updated_at in my queries to date(updated_at) like so:
month case:
select date(updated_at), sum(click_sum) from aggregated_clicks
where date(updated_at) >= date_sub(date_sub(curdate(), interval extract(day from (curdate())-1) day), interval 1 month)
and date(updated_at) < date_sub(curdate(), interval (extract(day from curdate())-1) day)
group by date(updated_at);
week case:
select date(updated_at), sum(click_sum) from aggregated_clicks
where date(updated_at) >= date_sub(curdate(), interval (dayofweek(curdate())+6) day)
and date(updated_at) <= date_sub(curdate(), interval dayofweek(curdate()) day)
group by date(updated_at);
yesterday case:
select date(updated_at), sum(click_sum) from aggregated_clicks
where date(updated_at) = date_sub(curdate(), interval 1 day)
group by date(updated_at);
use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.
I have a mysql table as follows:
week start_date end date
1 2011-04-25 2011-05-01
2 2011-05-02 2011-05-08
3 2011-05-09 2011-05-15
I would like to run a query to get the week number when the current date is between the start_date and end_date of a specified week.
SELECT week
FROM table_name
WHERE CURRENT_DATE() BETWEEN start_date AND end_date
CURRENT_DATE() is synonyms for CURDATE().
SELECT week FROM TABLE_NAME WHERE CURDATE() BETWEEN start_date AND end_date;
Pretty simple:
SELECT week FROM my_table WHERE CURRENT_DATE > start_date AND CURRENT_DATE <= end_date
I have an existing MySQL table with various fields.
userid | name | ... | timestamp
I'd like to have a statistics tool which shows me how many entrys were made in a specific day and in which hour of that day.
Example:
Statistics for: 02-24-2011
Total entrys: XXXXX
Hour 1: XX //Which should be 12 AM to 1 AM
Hour 2: XX
Hour 3: XX
...
How can this be done? Thanks for all your help!
DC
SELECT HOUR( timestamp ) AS the_hour, COUNT(*) AS entries
FROM your_table
WHERE DATE( timestamp ) = NOW()
GROUP BY HOUR( timestamp )
it will return counters hour by hour from today (change NOW() if need to change day), if no entry in specific hour -> no record,
Try that
SELECT DATE_FORMAT(timestamp, '%m-%d-%Y'), COUNT(userid) FROM your_table_name
WHERE DATE_FORMAT(timestamp, '%Y-%m-%d %H:%i:%s') >= '2011-02-24 00:00:00' AND DATE_FORMAT(timestamp, '%Y-%m-%d %H:%i:%s') <= '2011-02-24 23:59:59'
GROUP BY DATE_FORMAT(timestamp, '%Y-%m-%d %H')