Om du vill ha värdena på en enda rad kan du göra så här:
select max(playtime), min(playtime), avg(playtime)
from yourtable
group by mediaid;
Om du vill ha dem i separata rader, använd en UNION ALL
:
select max(playtime) playtime, mediaid, 'Max' Type
from yourtable
group by mediaid
union all
select min(playtime) playtime, mediaid, 'Min'
from yourtable
group by mediaid
union all
select avg(playtime) playtime, mediaid, 'Avg'
from yourtable
group by mediaid
order by mediaid, playtime;
se SQL-fiol med demo
Redigera #1, med hjälp av dina exempeldata:
select m.medTitle,
min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
on s.MedId = m.MedId
group by m.medTitle
union all
select m.medTitle,
avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
on s.MedId = m.MedId
group by m.medTitle
union all
select m.medTitle,
max(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m
on s.MedId = m.MedId
group by m.medTitle
order by medTitle, playtime