BEGIN

	select adb.project,
		adb.site,
		dbi.app_server,
        adb.instance,
        dbi.dns_name, dbi.dns_site, dbi.db_type, dbi.db_mode,
        adb.`status` as conn_status,
        adb.check_time as last_update_time,
        -- adb.last_failed_time,
        'A' as record_status,
        now() as last_sync_time
    from te3logex1.app_to_db_status adb
    inner join (
		select project, site, instance, max(check_time) as max_check_time
		from te3logex1.app_to_db_status 
        GROUP BY project, site, instance
	) adb1 on (adb.project = adb1.project and adb.site = adb1.site and adb.instance = adb1.instance and adb.check_time = adb1.max_check_time)
    inner join 
    (
		select distinct project, site, instance, app_server, dns_name, dns_site, db_type, db_mode
		from te3logex1.complete_db_info 
	) dbi on (adb.site = dbi.site and adb.project = dbi.project and adb.instance = dbi.instance)
    GROUP BY adb.site, adb.project, dbi.app_server, adb.instance, adb.check_time, adb.last_failed_time;

    
END