sql - MySql subquery: average difference, grouped by column -


i have mysql table visitor_id, country, time_of_visit.

i want average duration of visit country.

to duration, difference between earliest , latest time_of_visit each visitor_id.

so gets me average duration of visits:

    select avg(duration)          (     select timestampdiff(second, min(time_of_visit), max(time_of_visit))/60  duration      tracker      group visitor_id     ) tracker 

that works. group country, fail. here's recent attempt average duration country:

select country, avg(duration)  ( select timestampdiff(second, min(time_of_visit), max(time_of_visit))/60  duration  tracker  group visitor_id ) tracker group country 

the error is: unknown column 'country' in 'field list'.

i think should simple, i'm noob. searched lot, tried lots of ideas, no good. help?

thanks in advance.

you have select country column in subquery , have reference country field derived table tracker.country

select tracker.country, avg(tracker.duration)  ( select timestampdiff(second, min(time_of_visit), max(time_of_visit))/60      duration ,country tracker  group visitor_id ) tracker group tracker.country 

edit

using group by in subselect visitor_id gives record duplicate data countries , when using group by both visitor_id,country group data of countries within same visitor id, possible if 1 visitor belong more 1 countries , if 1 visitor belong 1 country i.e one-to-one relation use group visitor_id


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -