0

I need a SQL query using select statement that excludes duplicate rows that has the same Start_Time, End_Time, and ReasonCode

EventID       Start_Time         End_Time             ReasonCode 

 122         1/3/2015 0:27      1/3/2015 1:32      No Trouble Found
 123         1/3/2015 0:27      1/3/2015 1:32      No Trouble Found
 124         1/3/2015 0:27      1/3/2015 1:32      No Trouble Found
 125         1/31/2015 14:35    1/31/2015 14:56    LinkupDown

The resulting output would look like:

EventID       Start_Time         End_Time             ReasonCode 

 122         1/3/2015 0:27      1/3/2015 1:32      No Trouble Found
 125         1/31/2015 14:35    1/31/2015 14:56    LinkupDown

Thanks for the help guys.

dyao
  • 983
  • 3
  • 12
  • 25
  • 1
    Google for GROUP BY: https://www.google.ca/search?q=mysql%20group%20by%20example – Stan Apr 27 '15 at 19:45
  • 3
    distinct or group by are what you are looking for. distinct if you don't really care about EventID, and group by if you want a min(EventId) – Marshall Tigerus Apr 27 '15 at 19:48
  • Ah, you know guys I didn't even think about the group statement. I was figuring I would need to use a fancy `Case` statement to do this. – dyao Apr 27 '15 at 19:52
  • possible duplicate of [How can I remove duplicate rows?](http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows) – Paddy Apr 27 '15 at 19:55
  • The above doesn't work, I cannot make changes to the database tables. – dyao Apr 27 '15 at 20:06

1 Answers1

1

Applying the group by and the min function to the query will create the desired result set. SQL Fiddle Demo.

select min(eventid) EventID, DATE_FORMAT(Start_Time,'%m/%d/%Y %H:%i') Start_Time, DATE_FORMAT(End_Time,'%m/%d/%Y %H:%i') End_Time, ReasonCode
from mytable
group by ReasonCode
order by eventid
WorkSmarter
  • 3,738
  • 3
  • 29
  • 34