Get the first and last insertion of a user

1

I have the following query:

SQLFiddle

The question is as follows. In this table there are several interactions of several users in the same transaction. What I need is to get the last user lifetime in the transaction.

For example:

According to the table above, idusers 560049 was the last user to service the transaction. He began service at 2018-09-12T09: 34: 50Z and ended service at 2018-09-12T09: 54: 50Z. The service time in this case is 20 minutes.

What matters to me is always the last user of the transaction.

If I try to put the min and max gives problem because it groups and goes wrong.

And if it is the case the user does not have a end time, I just want to ignore it. As in the example below:

SQLFiddle

I know the question is complex, but I could not explain it any other way. If any moderator thinks it is unclear, let me know that I am trying to restate the issue.

    
asked by anonymous 12.09.2018 / 15:39

1 answer

0

One way to do it (which worked due to MySQL boundaries) would be subquery :

SELECT * 
FROM (SELECT idusers, MIN(created) mn, MAX(created) mx 
FROM obusuario 
GROUP BY idusers 
ORDER BY MAX(created)) tab 
ORDER BY mx DESC LIMIT 1
    
12.09.2018 / 16:19