WITH dt_e AS ( SELECT ss.ssa_code, ss.ssa_name, o.site_id, vendor, bts_tech, bts_down_ts, SUM(down_time) FROM a_bts_down_old o LEFT JOIN b_site si ON o.site_id = si.site_id LEFT JOIN b_ssa ss ON si.ssa_code = ss.ssa_code WHERE vendor = 'E' AND alarm_element = 'S' AND alarm_type = 'S' AND bts_down_ts >= '2017-09-12' AND bts_up_ts <= '2017-09-13' GROUP BY ssa_code, ssa_name, site_id, vendor, bts_tech, bts_down_ts HAVING COUNT(*) >= 2 ), dt_n AS ( SELECT ss.ssa_code, ss.ssa_name, o.site_id, vendor, bts_tech, bts_down_ts, SUM(down_time) FROM a_bts_down_old o LEFT JOIN b_site si ON o.site_id = si.site_id LEFT JOIN b_ssa ss ON si.ssa_code = ss.ssa_code WHERE vendor = 'N' AND alarm_element = 'B' AND alarm_type = 'S' AND bts_down_ts >= '2017-09-12' AND bts_up_ts <= '2017-09-13' GROUP BY ssa_code, ssa_name, site_id, vendor, bts_tech, bts_down_ts ) SELECT * FROM dt_e UNION ALL SELECT * FROM dt_n;