Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Estou tendo problemas ao usar o left join e group by simultaneamente. Estou tendo várias linhas de dados com o mesmo user_id , então quero usar group by


Você tem que agrupar por countersum também.
  create table call_log (
  user_id int(10), 
  duration int(10)      );

  insert into call_log values ( 5019,12),
                              ( 2345,23),
                              ( 5019,14);

  create table job_timetable  (
              user_id int(10), 
               counter int(10) );

  insert into job_timetable values ( 5019,1),
                                   ( 5019,3),
                                   ( 2345,2);


  SELECT call_log.user_id,SUM(call_log.duration) as duration,
                    avg(call_log.duration) as average_duration,
                                                    countersum
    FROM call_log 
    LEFT JOIN ( SELECT user_id,sum(counter) as countersum FROM job_timetable 
    GROUP BY user_id )  b   on call_log.user_id= b.user_id
    where call_log.user_id is not null 
    group by call_log.user_id,countersum 
    order by  call_log.user_id asc;

Demonstração:https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/77