2017-01-13 32 views
-1
select sync_date, max(sync_end_time),user_code from sync_tracker where 
sync_action = 'COMPLETION' 
and sync_status = 'SUCCESS' 
and user_code in (select ut.code from user_table ut 
    join user_location_mapping ulm 
    on ut.code = ulm.user_code) 
and sync_date = MAX(sync_date)-- can't even write aggregate function 
group by sync_date,user_code 
order by sync_date desc 

User_code recent_login_date recent_login_time 
0012  2016_11_11  11:30:23 
0013  2016_1_19   12:30:23 
0014  2016_12_4   1:30:23 

我希望最近的日期和時間,這些是不同的列。 請幫我找出相同的,它給錯誤,我應該使用「存在」作爲sbquery返回記錄倍數。如何從最大日期在mysql中的同一個表中獲得最大時間

+0

建議:使用'MAX(sync_date)而不是'你可以嘗試一個子查詢'SELECT MAX(sync_date)FROM USER_TABLE WHERE ' –

+0

感謝快速回復,和時間?不工作 – Sia

+1

你能告訴您的,請表的結構,一些數據和你預期的結果? – Philipp

回答

0
select st.user_code, tAux.dateMax, max(st.sync_end_time) as timeMax 
    from sync_tracker st 
    inner join 
    (
    select user_code, max(sync_date) as dateMax 
    from sync_tracker 
    inner join user_table ut on user_code = ut.code 
    inner join user_location_mapping ulm on ut.code = ulm.user_code 
    where sync_action = 'COMPLETION' 
    and sync_status = 'SUCCESS' 
    group by user_code, sync_date 
    ) tAux 
    on st.user_code = tAux.user_code and st.sync_date = tAux.dateMax 
    group by st.user_code, tAux.dateMax 
    order by dateMax, timeMax 
+0

感謝您爲您的文章@弗蘭塞雷佐,有時你不能找到答案,因爲場景不能解釋清楚,我連着MODIFIED_DATE在表中,這是幫我畫了答案。 – Sia

相關問題