I have two tables in a MySQL database, events and appointments. Each appointment has a field event_id, a start_time and an end_time column.
I'm trying to set up a query which will give me the event ids with their total amount of appointment time in minutes. For example, if an event has two appointments with the following details:
Starts at 10:00:00, Finishes at 11:30:00
Starts at 12:30:00, Finishes at 13:15:00
Then the appointments column for the event would read 135.
I know how to get a simple count of the appointments but I'm a bit stuck when it comes to working out the time. Here's what I have so far:
SELECT
events.id AS event_id,
(SELECT COUNT(id) FROM appointments WHERE event_id = events.id) AS appointment_count
FROM events
Any advice appreciated.
Thanks.
try something like this:
SELECT e.name, ap.event_id,
sum((TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))/60) AS dif
FROM events e LEFT JOIN appointment ap ON e.id = ap.event_id
GROUP BY e.name, ap.event_id
ORDER BY 2;
Example..
try this
SELECT A.*,count(B.*) as count
FROM table_event A
JOIN table_appointment B ON A.event_id = B.event_id
GROUP BY B.event_id
Related
I have two tables; one is called rules and the other data. The Rules table holds events, which have a description, id and date_created and is simply used to categorize events.
The data table has a date and id column; This stores the actual dates of an event, as events can span up to months long in dates.
My issue is this: I wish to select everything from data and group it by date, so each date is represented only once. However, the event with the most recent creation date should have precedence if there is a collision, i.e. two events happen on the same day. Here is what I've tried, which doesn't offer control over date_created:
SELECT d.date, r.description FROM data d LEFT JOIN rules r ON d.id = r.id GROUP BY date ORDER BY d.date
I haven't included date_created yet because I'm stuck, and not sure where it should go in the query to get the desired effect. Any ideas would be greatly appreciated!
From your question it seems to me that at first you need to select maximum date of event creation and then using subquery you can desired result:
SELECT a.date, b.description
FROM data a
INNER JOIN (
SELECT id, description,MAX(date_created) as mdate
FROM rules
GROUP BY id,description
) b ON a.id = b.id AND a.date = b.mdate
I have the following 2 tables:
Participants
deviceid, name
DeviceLocation
deviceid, gpslocation, timestamp
The DeviceLocation table is populated every few minutes with the latest GPS location of the device.
How can I get a list of all devices and the corresponding gps location and participant name with the latest timestamp?
SELECT deviceid,timestamp,gpslocation,name
FROM Participants p1
WHERE timestamp = (SELECT MAX(timestamp) FROM DeviceLocation d2 WHERE p1.deviceid = d2.deviceid)
GROUP BY deviceid;
Try this:
SELECT A.*, B.gpslocation, B.timestamp
FROM Participants A JOIN DeviceLocation B
ON A.deviceid=B.deviceid JOIN
(SELECT MAX(`timestamp`) max_timestamp
FROM DeviceLocation) C
ON B.`timestamp`=C.max_timestamp;
I think you could instead fetch the data base on time intervals since the rate at which the data are inserted might be high. You could try something like:
SELECT A.*, B.gpslocation, B.timestamp
FROM Participants A JOIN DeviceLocation B
ON A.deviceid=B.deviceid
WHERE B.`timestamp`>=NOW() - INTERVAL 10 MINUTE;
I have been trying to figure out a MySQL statement to perform the following.
I have a table with that stores jobs (tbl_jobs).
Another table that stores work scheduling (tbl_schedule) in the form of fixed time slots.
I want the resulting query to show all jobs scheduled today, check if the jobs are already scheduled (timeslot field) and return the earliest time slot.
My timeslots are stored as numbers from 1-8 so I used MIN to get the smallest number.
There can be the same job spanning multiple timeslots.
I tried a code from MySQL INNER JOIN select only one row from second table but I believe I don't understand the query in depth enough to make my own statement for my purposes
SELECT a.*, c.*
FROM tbl_jobs a
INNER JOIN tbl_schedule c
ON a.job_id = c.job_id
INNER JOIN (
SELECT job_id, MIN(timeslot) ts
FROM tbl_schedule
GROUP BY job_id
) b ON c.job_id = b.job_id
WHERE date = '2018-01-05'
This query on returns jobs that are scheduled and the ones that are not scheduled do not show up at all.
Would appreciate if anyone can assist me in where I should go from here? I am at a roadblock so, I decided to post here for help! Thanks in advance!
To get unscheduled job, use the left join
SELECT a.*, c.*
FROM tbl_jobs a
LEFT JOIN tbl_schedule c
ON a.job_id = c.job_id
LEFT JOIN (
SELECT job_id, MIN(timeslot) ts
FROM tbl_schedule
GROUP BY job_id AND
) b ON c.job_id = b.job_id
WHERE date = '2018-01-05'
I have a database containing two tables :
Events (id, name, is_active)
Event_logs (id, event_id, status, message)
I'd like to list all the events that had at least 3 different status logs in it and is still active (is_active=1) and I can't come up with the correct MySQL query to do that.
(I can add more details if needed, just ask :) ).
Here's the SQLFiddle : http://sqlfiddle.com/#!9/a7442
In that example, I'm looking to get the event ID 2 because it contains 3 different status in event_log : INFO, WARNING and ERROR.
Event ID 1 and 3 contains less than 3 and so are excluded.
Thank you for your help!
Maybe more coffee is required...
SELECT e.*
FROM events e
JOIN event_logs l
ON l.event_id = e.id
WHERE is_active=1
GROUP
BY e.id
HAVING COUNT(DISTINCT l.status) >=3
Didn't test but if I understand the requirement correctly, this should work
select e.id , l.status
from events e
inner join event_logs l
on e.id=l.event_id
where l.is_active=1
group by e.id,l.status
having count(*) >=3
I am really having trouble joining mulitple tables in a working query.
There are 5 different tables:
Event storing: eventid, staffnumber, date, start and end date..
Event_overview storing: eventid, clientid..
Rates storing: clientid, rateswaiter, rateschef, ratesteamleader..
staff: staffid, firstname, lastname..
salary: staffid, salary
I want to create one table giving me the result for all events grouped by staffid indicating the total number of hours they worked per event, the rate according to their role per hour and multiplied by the number of hours they worked, the salary, salary*hours
I have started with two different tables which work perfectly on their own.
Select event.staffid, staff.firstname,
staff.lastname, salary.wage, evento.clientid,
event.date, TIMEDIFF( hours, pause ) AS Total, event.role
from event
inner join evento on event.eventid=evento.eventid
inner join salary on event.staffid=salary.staffid
inner join staff on event.staffid=staff.staffid
The second query
SELECT event.clientid
FROM evento
JOIN rates ON evento.clientid = rates.clientid
group by evento.clientid
Later I want need to decide which rate to select based on the role of the staff member
CASE WHEN Position = 'Teamleader'
THEN (Teamleader)
WHEN Position = 'waiter'
THEN (waiter)
WHEN Position = 'chef'
THEN (chef)
ELSE '0'
END AS revenue
I want to have those information in one table so I can start mulitpliying and summing up per staff member and using above queries as subqueries.
Hope someone can help me.
thanks in advance,
I think something like this is what you want:
Select event.staffid, staff.firstname,
staff.lastname, salary.wage, evento.clientid,
event.date, TIMEDIFF( hours, pause ) AS Total, event.role,
TIMEDIFF(hours, pause) *
case position
when 'Teamleader' then rateteamleader
when 'waiter' then ratewaiter
when 'chef' then ratechef
end TotalSalary
from event
inner join evento on event.eventid=evento.eventid
inner join salary on event.staffid=salary.staffid
inner join staff on event.staffid=staff.staffid
inner join rates on evento.clientid = rates.clientid
It would be better if you normalized the rates table, with columns: clientid, position, rate. Then the join would be:
inner join rates on evento.clientid = rates.clientid and staff.position = rates.position
and you would just do TIMEDIFF(house, pause) * rate to get TotalSalary.