2017-01-13 4 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が複数のレコード数を返すときに 'EXISTS'を使うべきであるというエラーを出しています。mysqlの同じテーブルの最大日付から最大時間を取得する方法

+0

提案:代わりに 'MAX(sync_date)を使用しての' 'あなたはサブクエリを試すことができます迅速な応答のために、そして時間のためuser_table WHERE ' –

+0

おかげFROM MAX(sync_date)を選択しますか?動作していません – 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

関連する問題