I have a table that has records for multiple session events. Each row is an event in a session, and a session can have multiples of the same event. Those are basically game sessions and each event is round start or round end. My data looks something like
Session_id | Event_type | Event_Time
1 | round_start | 12:01:00
1 | round_end | 12:02:00
1 | round_start| 12:05:00
1 | round_end | 12:7:00
2 | round_start | 14:11:00
2 | round_end | 14:12:00
3 | round_start| 15:09:00
3 | round_end | 15:13:00
I am trying to find the average round duration. I tried the following SQL
select
RS.session_id,
RS.Event_Time as StartTime,
RE.EndTime,
TIMESTAMPDIFF(MINUTE,RE.EndTime,RS.Event_Time) as duration
from amp_event_mamlaka as RS
left join
(
select session_id, min(event_time) as EndTimd from amp_event_mamlaka
where Event_Type = "Round End" and session_id = RS.session_id and event_time>RS.Event_Time
) RE
on RE.session_id = RS.session_id
The issue is that I can't reference RS.session_id and RS.event_time in the joined table.
I am using MySQL. Any suggestions on how to accomplish this?
Thanks
I would suggest that you approach this with a correlated subquery:
select RS.session_id, RS.Event_Time as StartTime,
(select smin(event_time)
from amp_event_mamlaka em
where em.session_id = RS.session_id and
em.Event_Type = 'Round End' and
em.event_time > RS.Event_Time
) as EndTime,
from amp_event_mamlaka RS;
You can do the timestamp difference using a subquery:
select RS.*, TIMESTAMPDIFF(MINUTE, EndTime, Event_Time) as duration
from (select RS.session_id, RS.Event_Time as StartTime,
(select min(event_time)
from amp_event_mamlaka em
where em.session_id = RS.session_id and
em.Event_Type = 'Round End' and
em.event_time > RS.Event_Time
) as EndTime
from amp_event_mamlaka RS
) RS
Instead of keeping it in where
clause of subquery
you can keep the condition in Join On
clause. Try this.
SELECT RS.session_id,
RS.Event_Time AS StartTime,
RE.EndTime,
Timestampdiff(MINUTE, RE.EndTime, RS.Event_Time) AS duration
FROM amp_event_mamlaka AS RS
LEFT JOIN (SELECT session_id,
Min(event_time) AS EndTimd
FROM amp_event_mamlaka
WHERE Event_Type = "Round End") RE
ON RE.session_id = RS.session_id
AND RE.event_time > RS.Event_Time
A subquery, as opposed to a nested query, should only return one value. Your requirement is an example where you want data from pairs of rows. The subquery is only used to connect the pair, not supply data. Fiddle
select e1.SessionID, e1.EventType, e1.EventTime, e2.EventType, e2.EventTime, TimeStampDiff( minute, e1.EventTime, e2.EventTime ) Duration
from Events e1
join Events e2
on e2.SessionID = e1.SessionID
and e2.EventType = 'end'
and e2.EventTime =(
select Min( EventTime )
from Events
where SessionID = e1.SessionID
and EventType = 'end'
and EventTime > e1.EventTime )
where e1.EventType = 'start';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.