1

enter image description here

I want to list min time and max time of a day in for each users from the below table.

How to fetch records like below.

Expected result:

user_id  FROM                 TO
-------- -------------------  -------------------
6        2015-01-22 11:30:51  2015-01-22 16:30:51
17       2015-01-22 11:59:31  2015-01-22 11:59:31
17       2015-01-30 05:52:30  2015-01-30 06:07:47
Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58

1 Answers1

0

Try this. You didn't include a table name.

SELECT user_id, MIN(date) AS 'FROM', MAX(date) AS 'TO' FROM <table> GROUP BY user_id
Frank
  • 664
  • 5
  • 15
  • If you want the min/max "time" of the day: SELECT user_id, DATE_FORMAT(MIN(date), '%H:%i:%s') AS `FROM`, DATE_FORMAT(MAX(date), '%H:%i:s') AS `TO` FROM table GROUP BY user_id – ggioffreda Apr 09 '15 at 10:10
  • @ggioffreda Does it matter whether or not he uses DATE_FORMAT since the columns themselves are already in the format he wants? – Frank Apr 09 '15 at 10:11
  • 1
    I've edited the comment while you were replying :) it was just in case he actually meant "I want to list min time and max time of a day". I know it contradicts the expected result, but it doesn't harm – ggioffreda Apr 09 '15 at 10:16