dql - SQL Fetching sessions based on an interval of 30 minutes -


The title probably leaves some questions, so I explain in detail.

I have a Chatmate with a MySQL tablet, all these date-time columns of the date they were added to these messages.

Now that I want to receive, it is that I get the number of sessions that I had.

Now, what is a session? A new session begins when the last message is 30 minutes or longer.

For example with data:

  2014-01-01 00: 00:01 2014-01-01 00:20:01 2014-01-01 00 : 40: 01 2014-01-01 00:60:01   

will be a session

  2014-01-01 00:00:01 & lt ; - 2014-01-01 00:32:01 & lt; - 2014-01-01 00:35:01 2014-01- 01 01:00:01 2014-01-01 02:00:01 & lt; - 2014-01-01 02:20:01   

In my session, two sessions will be started Arrow at the start.

I do not need a DCL example, for example MySQL would be OK, I hope someone can help me.

Edit: The answer given below is to work in Bela, but our MySQL server is running at 5.5.4, if this is a fixed setting or sqlFiddle is not working just as objective So, I'm thinking.

Unfortunately, which is known as windowing functions (which most other major RDBMS does ), So we have a dummy up of our own, it is not really difficult at all, but it would be good to be support ...

Anyway, I had said that we should have the lag () Function, but it is true that we can compare a grouping counter by increasing, so we can actually cut one step, Sorta:

  SELECT sendAt, @ session: = IF (sentAt & lt; @essionboundary, @ session, @ session + 1) session, @ session limit: = ADDTIME (Sent, '00 : 30: 00 ') Join the session as session from session (SELECT @ session: = 0) sent by ORDER   

Serve serious, because it is a good practice to use a 'unique upper limit' ( & lt; ); positive continuous class types (such as), this 30th minute that actually starts your new session That is, the initial message at 13:00 means that the next session starts at 13:30 (no additional messages are given), this is a good asset to raise everything well, and I am curious about the behavior There is no need to worry about partial seconds, which I have not specified.
In any case, it gives results like this: Sent session session bounded 2014-01-01 00:00:01 1 2014-01-01 00:30: 01 2014-01-01 00:32:01 2 2014-01-01 01:02: 01 2014-01-01 00:35:01 2 01-01-01 01:05:01 2014-01-01 01: 00:01 2 2014-01-01 01:30:01 2014-01-01 02:00 00: 01 3 2014-01-01 02:30:01 2014-01-01 02:20:01 3 2014-01-31 01 02:50:01

Now, since you wanted how many sessions were s, you can wrap it as a subdivision:

  Select from Select Max (session) (Sent, @ session: = IF (sent & lt; @ session limit, @ session, @ session + 1) R, @ Session limit: = ADDTIME (Sent, '00: 30: 00') Message from session as session (SELECT @ession: = 0) sent by n command) Message session   


(Note: For some reason I do not understand Don, due to a subquery using initial work, Bella starts at 0 instead of 0 It was first please check it on your server, because you may need to start with @ session = 1 instead of 0, or something like Industry in (different sessions) ).

... and we've done it.


Although you are only listed for counting, once you have session grouping you have all kinds of fun with your data now maximum (sent) < / Code> / MIN (sentAt) To get a per group, how many messages in the group, or whatever, for example, you can do this by means of something like "all long runers Sessions can be found ":

  SELECT session, MIN (sentAt) First message, maximum (sent AS LastMessageAt, COUNT (*) AS Message (Send Sent, @ Session: = IF (Sent & Lt; @ Session Boundary, @ session, @ Session + 1) Session, @ Session Limit: = ADDTIME (Sent, '00: 30: 00 ') Message message from the message session (SELECT @ session: = 0) is sent by n order) Add message session through session session (minutes (sent),' 24: 00: 00 ') & Lt; Maximum (sent)   

(Find all sessions running for at least 24 hours)

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -