mysql - Select data from range between conversational openings and closings (chat log analysis using SQL / H2 Database) -


i have table named "conversations" , holds data this:

 timestamp       | actor| context| message    | classification -----------------+------+--------+------------+----------- 01.02.2015 09:38 | user | text   | blablabla  | normal 01.02.2015 09:46 | bot  | text   | blablabla  | normal 01.02.2015 10:19 | user | text   | hi bot!    | opening 01.02.2015 10:20 | bot  | text   | blablabla  | normal 01.02.2015 10:21 | user | text   | blablabla  | normal 01.02.2015 10:22 | bot  | text   | blablabla  | normal 01.02.2015 10:23 | user | text   | ok bye bot | closing 01.02.2015 11:53 | bot  | text   | blablabla  | normal 01.02.2015 12:14 | user | text   | goodbye    | closing 01.02.2015 12:33 | bot  | text   | blablabla  | normal 01.02.2015 12:51 | bot  | text   | blablabla  | normal 

what want select range between 'openings' , 'closings' in dataset in order separate framed conversations other user inputs / bot outputs.

the expected output be:

 timestamp       | actor| context| message    | classification -----------------+------+--------+------------+----------- 01.02.2015 10:19 | user | text   | hi bot!    | opening 01.02.2015 10:20 | bot  | text   | blablabla  | normal 01.02.2015 10:21 | user | text   | blablabla  | normal 01.02.2015 10:22 | bot  | text   | blablabla  | normal 01.02.2015 10:23 | user | text   | ok bye bot | closing 

----------- update

i executed query proposed in comments h2 didn't output data:

output_info

thank you!

to solve imagine date , need between open , close parenthesis so:

(open   date   close) 

from have: date >= open , date <= close

but case: open date1 close date2 close

you need check date2 has open without close: nearleftopen > nearleftclose

so final logic became:

sql demo:

select t1.* table1 t1 t1.timestamp >= (select max(if (classification='opening', timestamp, null))                        table1 t2                        t2.timestamp <= t1.timestamp)   , t1.timestamp <= (select min(if (classification='closing', timestamp, null))                        table1 t3                        t3.timestamp >= t1.timestamp)         , (select max(if (classification='opening', timestamp, null))        table1 t2        t2.timestamp <= t1.timestamp) >        (select max(if (classification='closing', timestamp, '1900-01-01'))        table1 t3        t3.timestamp < t1.timestamp) 

output

enter image description here

as can see change 1 normal opening , appear. based in data don't know how handle those. provide enough information border cases know how handle those.

this can solve adding condition inverting last condition make sure closing doesn't have other open before.

still code need further testing other cases.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -