I need to convert the following (sql in access) to mysql. It is a group by first query, but obv mysql can't handle first or last.
SELECT First([20121202].Date) AS FirstOfDate, First([20121202].Time) AS FirstOfTime, [20121202].HomeMatchingId, [20121202].AwayMatchingId, [20121202].RunningStatus
FROM 20121202
GROUP BY [20121202].HomeMatchingId, [20121202].AwayMatchingId, [20121202].RunningStatus
HAVING ((([20121202].RunningStatus)=1))
Basically I am after the time at which the game (hometeam v awayteam) goes in play (runningstatus = 1)
Thanks in advance
Not sure what the date is for, but what about using the regular aggregates?
SELECT
MIN(`Date`) AS `FirstOfDate`,
MIN(`Time`) AS `FirstOfTime`,
`HomeMatchingId`,
`AwayMatchingId`,
`RunningStatus`
FROM `20121202`
GROUP BY
`HomeMatchingId`,
`AwayMatchingId`,
`RunningStatus`
HAVING
`RunningStatus`=1
I assume that the purpose of first
is to get the earliest date. If there is at most one row per date, then you can do:
SELECT t.Date AS FirstOfDate, t.Time AS FirstOfTime,
t.HomeMatchingId, t.AwayMatchingId, t.RunningStatus
FROM `20121202` t join
(select HomeMatchingId, AwayMatchingId, min(date) as mindate
from `20121202`
WHERE RunningStatus = 1;
group by HomeMatchingId, AwayMatchingId
) tmin
on t.date = tmin.date and t.HomeMatchingId = tmin.HomeMatchingId and
tmin.AwayMatchingId and t.AwayMatchingId and
WHERE RunningStatus = 1;
I moved the condition on RunningStatus
to the where
clause, because that is more efficient.
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.