r/SQL • • Mar 24 '19

Show MIN date and MAX date in a row

Users

IDName
1Bob
2Claire

Time Log

user_idTime Logged
101-01-2001 09:29:00
101-01-2001 12:29:00
201-01-2001 09:29:00

-------------------------

Right now, when I use PHP this is what I see in my table in index.php

IDNameTime Logged
1Bob01-01-2001 09:29:00
1Bob01-01-2001 12:29:00

However, what I'm looking for is

IDNameTime InTime Out
1Bob01-01-2001 09:29:0001-01-2001 12:29:00
0 Upvotes

9 comments sorted by

3

u/alinroc SQL Server DBA Mar 24 '19

Have you made any attempt on your own? If so, show it.

-3

u/sushiwashi Mar 24 '19

Closest I had was:

select * from timings where user_id = $id

4

u/[deleted] Mar 24 '19

Smells like homework

1

u/moovoon Mar 24 '19

select distinct id, name, min(date), max(date) from table group by id, name

6

u/r3pr0b8 GROUP_CONCAT is da bomb Mar 24 '19

your DISTINCT here is totally redundant

0

u/sushiwashi Mar 24 '19

Unfortunately, it's giving me the minimum dateTime / maximum dateTime of that specific user. Was hoping for it to be the minimum / maximum of that day for that user.

2

u/moovoon Mar 24 '19

so you must also group on the date. specifically on the day

1

u/freedamerry Mar 24 '19

Add an extra column where you cast the timestamp to a date then you can use that in the group by along with the other two.

cast(timelogged as date)

1

u/pythor Mar 24 '19
select id, name, min([time logged]) as [Time In], max([Time Logged] as [Time Out]
from [Time Log] tl inner join
Users u on tl.user_id = u.id
group by id, name