Группировка разных столбцов в один ряд - PullRequest
0 голосов
/ 14 июня 2019

Как сделать одну строку в разных строках и столбце

SELECT  
    internal_id, store_id, user_id, shift_info,
    DATE(log_datetime) dates,   
    (case when log_action = '0' then TIME(log_datetime) end) as time_in,
    (case when log_action = '0' then CONCAT(log_lat, ",", log_lng) end) as loc_in,  
    (case when log_action = '1' then TIME(log_datetime) end) as time_out,   
    (case when log_action = '1' then CONCAT(log_lat, ",", log_lng) end) as loc_out 
FROM
    attendance_store_user   
WHERE 
    user_id = "A4CBD64F-D21C-5612-CCF5-497892B62E76"

enter image description here

Я хочу получить такой результат:

enter image description here

1 Ответ

1 голос
/ 14 июня 2019

Вы можете попробовать использовать объединение одного и того же табличного фильтра для нулевых time_out и time_in

select  a.dates, a.store_id,  a,time_in, b.time_out 
FROM attendance_store_user a 
INNER JOIN attendance_store_user b on a.dates = b.dates 
  and a.user_id = b.user_id 
    and a.time_out is null 
      and b.time_in is null
WHERE a.user_id = "A4CBD64F-D21C-5612-CCF5-497892B62E76"
...