Ordering event data
I have a table which holds event data. The structure is the following
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`evt` varchar(20),
`sequence` int);
But it turns out that data is not coming in the correct order every time.
The correct order is decided by the following rules:
1. Every event belongs in a session. You can't have an EventStart, EventEnd outside a SessionStart, SessionEnd.
2. Each session starts with a SessionStart ending with SessionEnd.
3. An EventStart cannot appear later than an EventEnd.
4. The xStart, xEnd is matched using the starting time
5. The order of events and sessions is decided by time and sequence.
In every session there are a lot of events EventStart, EventEnd. Ideally we would expect them to happen in the following order:
1. SessionStart - 12 - 2018-02-10 15:50:00
2. EventStart - 0 - 2018-02-10 15:51:00
3. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
4. EventStart - 0 - 2018-02-10 15:52:00
5. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
6. SessionEnd - 13 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
But what if I get something like the following
1. EventStart - 0 - 2018-02-10 15:51:00
2. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
3. EventStart - 0 - 2018-02-10 15:52:00
4. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
5. SessionEnd - 12 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
6. SessionStart - 11 - 2018-02-10 15:50:00
I want to be able to collect multiple sessions with the correct order. Is this possible using a single query?
https://www.db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/2
mysql
|
show 3 more comments
I have a table which holds event data. The structure is the following
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`evt` varchar(20),
`sequence` int);
But it turns out that data is not coming in the correct order every time.
The correct order is decided by the following rules:
1. Every event belongs in a session. You can't have an EventStart, EventEnd outside a SessionStart, SessionEnd.
2. Each session starts with a SessionStart ending with SessionEnd.
3. An EventStart cannot appear later than an EventEnd.
4. The xStart, xEnd is matched using the starting time
5. The order of events and sessions is decided by time and sequence.
In every session there are a lot of events EventStart, EventEnd. Ideally we would expect them to happen in the following order:
1. SessionStart - 12 - 2018-02-10 15:50:00
2. EventStart - 0 - 2018-02-10 15:51:00
3. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
4. EventStart - 0 - 2018-02-10 15:52:00
5. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
6. SessionEnd - 13 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
But what if I get something like the following
1. EventStart - 0 - 2018-02-10 15:51:00
2. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
3. EventStart - 0 - 2018-02-10 15:52:00
4. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
5. SessionEnd - 12 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
6. SessionStart - 11 - 2018-02-10 15:50:00
I want to be able to collect multiple sessions with the correct order. Is this possible using a single query?
https://www.db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/2
mysql
In the sql fiddle data, how do you identify the event id of a particular row. There is no such information in the sample data .
– Madhur Bhaiya
Nov 21 '18 at 11:05
I cannot see any data in this fiddle.
– P.Salmon
Nov 21 '18 at 11:06
@P.Salmon SQL fiddle is unrealiable. I have copied to db fiddle: db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/0
– Madhur Bhaiya
Nov 21 '18 at 11:09
@MadhurBhaiya TheSessionEndhas the same start withSessionStart.
– kechapito
Nov 21 '18 at 11:33
There can be multiple SessionEnd; how does someone know which SessionStart it belongs to
– Madhur Bhaiya
Nov 21 '18 at 11:38
|
show 3 more comments
I have a table which holds event data. The structure is the following
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`evt` varchar(20),
`sequence` int);
But it turns out that data is not coming in the correct order every time.
The correct order is decided by the following rules:
1. Every event belongs in a session. You can't have an EventStart, EventEnd outside a SessionStart, SessionEnd.
2. Each session starts with a SessionStart ending with SessionEnd.
3. An EventStart cannot appear later than an EventEnd.
4. The xStart, xEnd is matched using the starting time
5. The order of events and sessions is decided by time and sequence.
In every session there are a lot of events EventStart, EventEnd. Ideally we would expect them to happen in the following order:
1. SessionStart - 12 - 2018-02-10 15:50:00
2. EventStart - 0 - 2018-02-10 15:51:00
3. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
4. EventStart - 0 - 2018-02-10 15:52:00
5. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
6. SessionEnd - 13 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
But what if I get something like the following
1. EventStart - 0 - 2018-02-10 15:51:00
2. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
3. EventStart - 0 - 2018-02-10 15:52:00
4. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
5. SessionEnd - 12 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
6. SessionStart - 11 - 2018-02-10 15:50:00
I want to be able to collect multiple sessions with the correct order. Is this possible using a single query?
https://www.db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/2
mysql
I have a table which holds event data. The structure is the following
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`evt` varchar(20),
`sequence` int);
But it turns out that data is not coming in the correct order every time.
The correct order is decided by the following rules:
1. Every event belongs in a session. You can't have an EventStart, EventEnd outside a SessionStart, SessionEnd.
2. Each session starts with a SessionStart ending with SessionEnd.
3. An EventStart cannot appear later than an EventEnd.
4. The xStart, xEnd is matched using the starting time
5. The order of events and sessions is decided by time and sequence.
In every session there are a lot of events EventStart, EventEnd. Ideally we would expect them to happen in the following order:
1. SessionStart - 12 - 2018-02-10 15:50:00
2. EventStart - 0 - 2018-02-10 15:51:00
3. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
4. EventStart - 0 - 2018-02-10 15:52:00
5. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
6. SessionEnd - 13 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
But what if I get something like the following
1. EventStart - 0 - 2018-02-10 15:51:00
2. EventEnd - 2 - 2018-02-10 15:51:00 - 2018-02-10 15:52:00
3. EventStart - 0 - 2018-02-10 15:52:00
4. EventEnd - 3 - 2018-02-10 15:52:00 - 2018-02-10 15:53:00
5. SessionEnd - 12 - 2018-02-10 15:50:00 - 2018-02-10 15:55:00
6. SessionStart - 11 - 2018-02-10 15:50:00
I want to be able to collect multiple sessions with the correct order. Is this possible using a single query?
https://www.db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/2
mysql
mysql
edited Nov 22 '18 at 7:09
kechapito
asked Nov 21 '18 at 10:58
kechapitokechapito
1,20232042
1,20232042
In the sql fiddle data, how do you identify the event id of a particular row. There is no such information in the sample data .
– Madhur Bhaiya
Nov 21 '18 at 11:05
I cannot see any data in this fiddle.
– P.Salmon
Nov 21 '18 at 11:06
@P.Salmon SQL fiddle is unrealiable. I have copied to db fiddle: db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/0
– Madhur Bhaiya
Nov 21 '18 at 11:09
@MadhurBhaiya TheSessionEndhas the same start withSessionStart.
– kechapito
Nov 21 '18 at 11:33
There can be multiple SessionEnd; how does someone know which SessionStart it belongs to
– Madhur Bhaiya
Nov 21 '18 at 11:38
|
show 3 more comments
In the sql fiddle data, how do you identify the event id of a particular row. There is no such information in the sample data .
– Madhur Bhaiya
Nov 21 '18 at 11:05
I cannot see any data in this fiddle.
– P.Salmon
Nov 21 '18 at 11:06
@P.Salmon SQL fiddle is unrealiable. I have copied to db fiddle: db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/0
– Madhur Bhaiya
Nov 21 '18 at 11:09
@MadhurBhaiya TheSessionEndhas the same start withSessionStart.
– kechapito
Nov 21 '18 at 11:33
There can be multiple SessionEnd; how does someone know which SessionStart it belongs to
– Madhur Bhaiya
Nov 21 '18 at 11:38
In the sql fiddle data, how do you identify the event id of a particular row. There is no such information in the sample data .
– Madhur Bhaiya
Nov 21 '18 at 11:05
In the sql fiddle data, how do you identify the event id of a particular row. There is no such information in the sample data .
– Madhur Bhaiya
Nov 21 '18 at 11:05
I cannot see any data in this fiddle.
– P.Salmon
Nov 21 '18 at 11:06
I cannot see any data in this fiddle.
– P.Salmon
Nov 21 '18 at 11:06
@P.Salmon SQL fiddle is unrealiable. I have copied to db fiddle: db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/0
– Madhur Bhaiya
Nov 21 '18 at 11:09
@P.Salmon SQL fiddle is unrealiable. I have copied to db fiddle: db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/0
– Madhur Bhaiya
Nov 21 '18 at 11:09
@MadhurBhaiya The
SessionEnd has the same start with SessionStart.– kechapito
Nov 21 '18 at 11:33
@MadhurBhaiya The
SessionEnd has the same start with SessionStart.– kechapito
Nov 21 '18 at 11:33
There can be multiple SessionEnd; how does someone know which SessionStart it belongs to
– Madhur Bhaiya
Nov 21 '18 at 11:38
There can be multiple SessionEnd; how does someone know which SessionStart it belongs to
– Madhur Bhaiya
Nov 21 '18 at 11:38
|
show 3 more comments
1 Answer
1
active
oldest
votes
This proved to be a difficult problem to solve using SQL only and I ended up doing a lot of changes.
What I did was to add another column named sessionId.
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`sessionId` int,
`evt` varchar(20),
`sequence` int);
This column gets populated by a background worker and assigns the same sessionId for every event inside the session period.
Then I can just use the following
SELECT * FROM t1 WHERE evt = 'SessionStart' AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt in('EventStart', 'EventEnd') AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt = 'SessionEnd' AND sessionId = 1
This could be further simplified because the newer devices that generate events will also include event generation timestamp and sessionId for every event.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410639%2fordering-event-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This proved to be a difficult problem to solve using SQL only and I ended up doing a lot of changes.
What I did was to add another column named sessionId.
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`sessionId` int,
`evt` varchar(20),
`sequence` int);
This column gets populated by a background worker and assigns the same sessionId for every event inside the session period.
Then I can just use the following
SELECT * FROM t1 WHERE evt = 'SessionStart' AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt in('EventStart', 'EventEnd') AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt = 'SessionEnd' AND sessionId = 1
This could be further simplified because the newer devices that generate events will also include event generation timestamp and sessionId for every event.
add a comment |
This proved to be a difficult problem to solve using SQL only and I ended up doing a lot of changes.
What I did was to add another column named sessionId.
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`sessionId` int,
`evt` varchar(20),
`sequence` int);
This column gets populated by a background worker and assigns the same sessionId for every event inside the session period.
Then I can just use the following
SELECT * FROM t1 WHERE evt = 'SessionStart' AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt in('EventStart', 'EventEnd') AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt = 'SessionEnd' AND sessionId = 1
This could be further simplified because the newer devices that generate events will also include event generation timestamp and sessionId for every event.
add a comment |
This proved to be a difficult problem to solve using SQL only and I ended up doing a lot of changes.
What I did was to add another column named sessionId.
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`sessionId` int,
`evt` varchar(20),
`sequence` int);
This column gets populated by a background worker and assigns the same sessionId for every event inside the session period.
Then I can just use the following
SELECT * FROM t1 WHERE evt = 'SessionStart' AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt in('EventStart', 'EventEnd') AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt = 'SessionEnd' AND sessionId = 1
This could be further simplified because the newer devices that generate events will also include event generation timestamp and sessionId for every event.
This proved to be a difficult problem to solve using SQL only and I ended up doing a lot of changes.
What I did was to add another column named sessionId.
CREATE TABLE t1 (`id` int,
`start` datetime,
`end` datetime default null,
`sessionId` int,
`evt` varchar(20),
`sequence` int);
This column gets populated by a background worker and assigns the same sessionId for every event inside the session period.
Then I can just use the following
SELECT * FROM t1 WHERE evt = 'SessionStart' AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt in('EventStart', 'EventEnd') AND sessionId = 1
UNION
SELECT * FROM t1 WHERE evt = 'SessionEnd' AND sessionId = 1
This could be further simplified because the newer devices that generate events will also include event generation timestamp and sessionId for every event.
answered Dec 14 '18 at 9:34
kechapitokechapito
1,20232042
1,20232042
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410639%2fordering-event-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

In the sql fiddle data, how do you identify the event id of a particular row. There is no such information in the sample data .
– Madhur Bhaiya
Nov 21 '18 at 11:05
I cannot see any data in this fiddle.
– P.Salmon
Nov 21 '18 at 11:06
@P.Salmon SQL fiddle is unrealiable. I have copied to db fiddle: db-fiddle.com/f/ipLSGJd5Pqzy1VBGXKx9Mv/0
– Madhur Bhaiya
Nov 21 '18 at 11:09
@MadhurBhaiya The
SessionEndhas the same start withSessionStart.– kechapito
Nov 21 '18 at 11:33
There can be multiple SessionEnd; how does someone know which SessionStart it belongs to
– Madhur Bhaiya
Nov 21 '18 at 11:38