time query for vb6 and msaccess - ms-access

Query for VB6 and MS Access
Table:-
User Id LogDate LogTime
1 1/1/2010 9:00
1 1/1/2010 10:00
1 1/1/2010 11:29
1 2/1/2010 10:00
2 2/1/2010 22:00
2 3/1/2010 11:00
Need to display as:-
User Id LogDate LogTime LogDate LogTime
1 1/1/2010 9:00 1/1/2010 10:00
1 1/1/2010 11:29 2/1/2010 10:00
2 2/1/2010 22:00 3/1/2010 11:00

You need to use a sub-query to find the next date for a user, something like:
SELECT tblStackOverflowTimeQuery.lngUserId, tblStackOverflowTimeQuery.datLogDateTime,
(SELECT TOP 1 tblStackOverflowTimeQuery2.datLogDateTime
FROM tblStackOverflowTimeQuery AS tblStackOverflowTimeQuery2
WHERE tblStackOverflowTimeQuery.lngUserId = tblStackOverflowTimeQuery2.lngUserId
AND tblStackOverflowTimeQuery2.datLogDateTime > tblStackOverflowTimeQuery.datLogDateTime
ORDER BY tblStackOverflowTimeQuery2.datLogDateTime
) AS datEndDateTime
FROM tblStackOverflowTimeQuery
ORDER BY tblStackOverflowTimeQuery.lngUserId, tblStackOverflowTimeQuery.datLogDateTime;
That'll give the following:
lngUserId datLogDateTime datEndDateTime
1 01/01/2010 09:00:00 01/01/2010 10:00:00
1 01/01/2010 10:00:00 01/01/2010 11:29:00
1 01/01/2010 11:29:00 02/01/2010 10:00:00
1 02/01/2010 10:00:00
2 02/01/2010 22:00:00 03/01/2010 11:00:00
2 03/01/2010 11:00:00
which is not exactly what you wanted, but it's a start.
It would be easiest to do these things using code, but that would depend on how you want to use it. Presumably there's nothing in your data to indicate which entry is the start and which is the end.

Related

Get dates from weekdays between 2 dates MySQL future schedule

Im trying to create a schedule table per week of year with MySQL
I have a table with the following Sample data:
employee
start_date
end_date
weekday
shift_start
shift_end
1
2021-10-01
2021-10-31
0
08:00:00
17:00:00
1
2021-10-01
2021-10-31
2
08:00:00
17:00:00
1
2021-10-01
2021-10-31
4
08:00:00
17:00:00
2
2021-10-01
2021-10-31
0
08:00:00
17:00:00
2
2021-10-01
2021-10-31
3
08:00:00
17:00:00
2
2021-10-01
2021-10-31
4
08:00:00
17:00:00
The schedule is the same every week, hence the weekday column.
I also have a calendar table for simplicity with dates for coming 2 years with the following schema:
fulldate
year
month
day
week
dayofweek
0000-00-00
0000-9999
0-11
0-30
0-53
0-6
I want to have a table output like the following for a certain weeknumber: (example week 40 of 2021)
date
employee
weekday
shift_start
shift_end
2021-10-04
1
0
08:00:00
17:00:00
2021-10-04
2
0
08:00:00
17:00:00
2021-10-06
1
2
08:00:00
17:00:00
2021-10-07
2
3
08:00:00
17:00:00
2021-10-08
1
4
08:00:00
17:00:00
2021-10-08
2
4
08:00:00
17:00:00
I have been stuck on this for a while and I haven't found a solution on the internet or made a single step in creating the table. Any help in the right direction would be greatly appreciated
Solution for your problem is:
SELECT
c.fulldate as date,
t.employee,
t.weekday,
t.shift_start,
t.shift_end
FROM calendar AS c
INNER JOIN table1 AS t
ON c.dayofweek = t.weekday
ORDER BY c.fulldate,t.employee,t.weekday
Demo Link: http://sqlfiddle.com/#!9/4c8681/4
Explanation:
Calendar table should be inner joined with input table on weekday column to get those dates for which result is required.
'WHERE' clause can be used to filter out the result as per the requirement. For example, if output is required for 40th week then use "WHERE c.week=40" in above query before 'ORDER BY' clause to get desired result.

How to get particular column data from mysql table?

I have table like this,
tid vid emp_id stop_time dates
----------------------------------------
1 5680 6 01:00 am 2017-05-19
2 5680 6 04:00 am 2017-05-19
3 5680 3 07:00 am 2017-05-19
4 5680 3 05:00 pm 2017-05-19
5 5680 6 08:00 am 2017-05-20
I want the particular value, for above this e.g i need this values 04:00 am and 05:00 pm and 08:00 am. That means i need last enter value of emp_id also particular dates based.
Demo data:
http://sqlfiddle.com/#!9/3f6e59/1
use subquery
select * from third_table
where tid in (select max(tid) from third_table
where dates between '2017-05-19' and '2017-05-20' group by emp_id,dates)

SQL return last 12 weeks data, grouped by week, starting last Monday

I've been working on a MySQL query that sorts data into weeks but I just can't figure out how to do it.
I would like to sort the data into weeks for the current and last 11 weeks. Each week will run from Monday 00:00:00 to Sunday 23:59:59.
(Taking todays date as 2014-12-04)...
Week 1: 2014-12-01 > 2014-12-07 - (Last Monday 00:00:00 to next Sunday 23:59:59)
Week 2: 2014-11-24 > 2014-11-30 - (Monday before last 00:00:00 to last Sunday 23:59:59)
Week 3: 2014-11-17 > 2014-11-23 - (Monday before before last 00:00:00 to last last Sunday 23:59:59)
And so on...
For each week the value field data will be totalled.
I need the data returned to be in the format:
datetime: The first date (Always a Monday) of that week.
value: The total of all the values in that week.
For example, the returned data:
Week 1: 2014-12-01 : Totalled value=11
Week 2: 2014-11-24 : Totalled value=3
Week 3: 2014-11-17 : Totalled value=9
Week 4: 2014-11-10 : Totalled value=7
Table_1 data:
table1id datetime value
1 2014-09-01 06:00:00 4
2 2014-09-04 17:00:00 6
3 2014-09-09 18:00:00 9
4 2014-09-15 07:00:00 4
5 2014-09-20 10:00:00 2
6 2014-09-25 10:00:00 3
7 2014-09-30 09:00:00 8
8 2014-10-01 14:00:00 5
9 2014-10-05 10:00:00 7
10 2014-10-09 18:00:00 3
11 2014-10-15 05:00:00 4
12 2014-10-20 07:00:00 8
13 2014-10-24 16:00:00 9
14 2014-10-29 15:00:00 5
15 2014-10-31 16:00:00 7
16 2014-11-05 09:00:00 2
17 2014-11-10 08:00:00 4
18 2014-11-15 16:00:00 3
19 2014-11-20 10:00:00 9
20 2014-11-25 10:00:00 2
21 2014-11-30 10:00:00 1
22 2014-12-01 15:00:00 7
23 2014-12-04 18:00:00 2
I 'could' just pull all the data unsorted for the date range using PHP and sort it from there but I'd rather the MySQL server do it.
Any suggestions would be greatly appreciated. :-)
based on generate days from date range
you can do smething like that:
select mondays.week, mondays.day, sum(value)
from
(select a.a+1 week, curdate() - WEEKDAY(curdate()) - INTERVAL (7*a.a) DAY as day from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select 10 union all select 11) as a) as mondays,
Table_1
where Table_1.datetime between mondays.day and (mondays.day + interval(7) day)
group by mondays.week, mondays.day;

Get 30 minutes interval data between start and end time in mysql

I have table structure in mysql,
table_id no_people booking_date bookingend_time bookingstart_time
14 2 2014-10-31 2014-10-31 13:30:00 2014-10-31 11:00:00
5 4 2014-10-31 2014-10-31 16:30:00 2014-10-31 14:30:00
6 2 2014-10-31 2014-10-31 17:00:00 2014-10-31 16:00:00
2 4 2014-11-06 2014-11-06 12:30:00 2014-11-06 10:00:00
2 4 2014-10-31 2014-10-31 16:00:00 2014-10-31 14:00:00
3 4 2014-11-01 2014-11-01 09:00:00 2014-11-01 07:30:00
6 2 2014-11-01 2014-11-01 10:00:00 2014-11-01 07:30:00
2 4 2014-11-03 2014-11-03 10:30:00 2014-11-03 08:30:00
5 4 2014-11-04 2014-11-04 10:30:00 2014-11-04 08:30:00
3 4 2014-11-05 2014-11-05 09:30:00 2014-11-05 07:30:00
14 2 2014-11-05 2014-11-05 09:30:00 2014-11-05 07:30:00
I want to retrieve table_id data with 30 minutes of interval between start and end time.
Ex:
if i give booking start time 10:30 and end time 12:30 i should get 14 as row..
Similarly it should check all rows and return between two times ..
My query so far
SELECT `table_id` FROM `booking` WHERE bookingstart_time>='2014-10-31 10:30:00' AND bookingend_time<='2014-10-31 11:30:00'
Step 1: expand the input time frame by 30 minutes before and 30 minutes after. DATE_ADD() and DATE_SUB() can do that:
DATE_SUB(_input_start_date_here_, INTERVAL 30 MINUTE)
Step 2: rethink your problem in terms of start and end times. Here are the possible cases:
if the booking started during the (expanded) period, then you want this booking in your result
or if the booking started before the period, then you want this booking unless it also ended before the period
on the other hand, if the booking started after the period, then you do not want this booking
The first situation above could be expressed like this:
WHERE bookingstart_time >= DATE_SUB(_input_start_date_here_, INTERVAL 30 MINUTE)
AND bookingstart_time <= DATE_ADD(_input_end_date_here_, INTERVAL 30 MINUTE)
The second condition is left as an exercise. You can also rewrite the above with a more elegant BETWEEN operator.
SELECT restaurant_table FROM rest_restaurantbooking WHERE TIMESTAMPDIFF(SECOND, bookingstart_time, bookingend_time) > 1800.
FOR REFERENCE: HERE

How to get available time for doctor from todays appointments?

I am working on a project in a company. I am trying to solve this query but I could not.
My tables are:
Appointments:
doctorId patientId patientName fromDateTime toDateTime
-------- --------- ----------- --------------------- ---------------------
56 1 fghfgh 3/23/2012 12:15:00 PM 3/23/2012 01:15:00 PM
56 2 asdadf 3/23/2012 01:15:00 PM 3/23/2012 02:15:00 PM
56 3 werwr 3/23/2012 09:15:00 AM 3/23/2012 10:15:00 AM
57 4 uiyui 3/23/2012 09:15:00 AM 3/23/2012 10:15:00 AM
57 5 asdad 3/23/2012 01:15:00 PM 3/23/2012 02:15:00 PM
This is my timeSchedule table:
id startTime endTime
-- ------------ ------------
1 08:00:00.000 09:00:00.000
2 09:00:00.000 10:00:00.000
3 11:00:00.000 12:00:00.000
4 12:00:00.000 13:00:00.000
5 13:00:00.000 14:00:00.000
6 14:00:00.000 15:00:00.000
7 15:00:00.000 16:00:00.000
Actually there are more values but I think these are enough to solve the problem.
I am comparing patient appointments with this timeSchedule table.
Now suppose if I pass parameter doctorId as 56 and consider today is 23 March then output should be like this:
id startTime endTime
-- --------- --------
1 08:00 AM 09:00 AM
3 11:00 AM 12:00 PM
6 02:00 PM 03:00 PM
7 03:00 PM 04:00 PM
How can I achieve the above result?
Assuming that timeSchedule.startTime and timeSchedule.endTime are both Time data types then it would be something like this...: (if not, you could cast as such).
DECLARE #pDoctorID Int = 56
DECLARE #pDate Date = '3/23/2012'
SELECT * FROM timeSchedule WHERE
NOT Exists (
SELECT doctorid FROM Appointments
WHERE doctorid = #pDoctorID AND
CAST(fromDatetime as Date) = #pDate AND
(
(CAST(fromDatetime as Time) >= timeSchedule.startTime AND
CAST(fromDatetime as Time) <= timeSchedule.endTime)
OR
(CAST(toDatetime as Time) >= timeSchedule.startTime AND
CAST(toDatetime as Time) <= timeSchedule.endTime)
OR
(CAST(toDatetime as Time) <= timeSchedule.startTime AND
CAST(fromDatetime as Time) >= timeSchedule.endTime)
)
)
Which with your sample data returns this:
1 | 08:00:00.00 | 09:00:00.00
4 | 11:00:00.00 | 12:00:00.00
8 | 15:00:00.00 | 16:00:00.00
In essence the query is saying give me any appointment for this doctor where existing appoints do not start or end between the time frames, or start before and end after any of the time slots defined by the timeSchedule table.
Formatting the return times is also a simple matter. See the table in this link for all your options.