select id = max(id), topic, category, date = max(date) into #tmp from alarms group by topic, category select t1.topic, t1.category, t1.date, t2.category, t2.date from #tmp t1, #tmp t2 where t1.category = "WARNING" and t2.category = "OK NOW" and t1.topic = t2.topic and t1.date < t2.date UNION select t1.topic, t1.category, t1.date, NULL, NULL from #tmp t1, #tmp t2 where t1.category = "WARNING" and t2.category = "OK NOW" and t1.topic = t2.topic and t1.date > t2.date