sql - Generate a Fire Register report -


I am stuck in a working SQL question for fallowing:
I generate a fire register report (How many people are still inside the building) on ​​the basis of an entry database that records logout / logout events with some metadata. Access DB looks like this:

 + ---- + --------------------- + -------- - + --------- + ------ + | ID | Date | Action | Success | User | + ---- + --------------------- + --------- + --------- + - - --- + | 1 | 2009-04-28 02: 00:00 | Login | 1 | Nick | | | 2 | 2009-04-28 03:00:00 | Logout | 1 | Nick | | | 3 | 2009-04-28 04:00:00 | Login | 1 | Nick | | | 4 | 2009-04-28 04:00:00 | Logout | 1 | Nick | | | 5 | 2009-04-28 04:00:00 | Login | 1 | Nick | | | 6 | 2009-04-28 07:00:00 | Login | 1 | John | | 7 | 2009-04-28 07:30:00 | Login | 1 | Sue | | 8 | 2009-04-28 08:00:00 | Logout | 1 | John | + ---- + --------------------- + --------- + --------- + - - --- + 

There may be multiple login / logout actions during the day when the administrator runs the report, it is only limited to the current day and a list of all users where this The last known action for the user is login and success = 1, which means that this person is currently in the building.

On the above given data, Nick and lawsuit should still be reported inside the building.

Another approach to the problem:

  SELECT T1.user Some of the few qualified T1 join the left OUTER Some_Table T2 ON T2 user = T1.user and T2.success = 1 and T2.date & gt; T1.date where T1.success = 1 and T1.action = 'Login' and T2.id is Null  

It considers that you care only about successful actions. If a user has two actions on the same exact date and time, then it can not work as expected.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -