I have a USERS table.
Each user has connections in a CONNECTIONS table.
Each connection has a datetime and some referenced properties like timezone, stored in a TZ reference table.
I’d like to select the userID, and the TimeZoneLabel for the first and the last connection. Even if a user has no connection (so NULL or anything else would be displayed)
Do something like :
Select USERS.id,
min(TZ.label),
max(TZ.label)
from USERS
join CONNECTION on USERS.id = CONNECTIONS.userid
join TZ on TZ.id = CONNECTIONS.tzid
group by USERS.id
order by max(CONNECTIONS.dateconn)
But I can’t achieve doing that. I’ve found articles on the net about that, but nothing works when I try. The example above does not work for the label, as there are no real min / max values but the one used on the first CONNECTION and the one used on the last one.
And I have many of these in my real request so I’d like to avoid too many sub-select.
Without the timezones:
With timezones (assuming
Connectiontable hasidasPrimary Key):A compound
(userid, dateconn, id)index onConnectiontable would help performance.