Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to write a query to group my two columns based on third column and show result in a row as shown below in SQLite CAn anybody guide me how can I write query for such structure Table Structure

CREATE TABLE `Table1` (
`Variable_col`  TEXT,
`TimeComes_Col` datetime,
`TimeGoes_Col`  datetime)

Dummy Data

Insert Into Table1 Values('var1','2017-03-16 10:00:00',''),('var1','','2017-03-16 10:05:00'),('var1','2017-03-16 10:10:00',''),('var2','2017-03-16 10:15:00','')

Tried So far but not getting result as wanted

WITH RECURSIVE cte1 as(select Variable_Col,TimeComes_Col, (SELECT COUNT()+1 FROM ( SELECT DISTINCT TimeComes_Col FROM Table1 AS t WHERE TimeComes_Col < Table1.TimeComes_Col )) as rn from Table1 where TimeComes_Col <> ''),

cte2 as(select TimeGoes_Col, (SELECT COUNT()+1 FROM (SELECT DISTINCT TimeGoes_Col FROM Table1 AS t WHERE TimeGoes_Col < Table1.TimeGoes_Col ) )as rn from Table1 where TimeGoes_Col <> '')select Variable_Col, TimeComes_Col, TimeGoes_Col from cte1 a left join cte2 b on a.rn = b.rn

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
170 views
Welcome To Ask or Share your Answers For Others

1 Answer

If these are always coming through in increments of 5 minutes

SELECT t1.variable_col, t1.timecomes_col, t2.timegoes_col
FROM Table1 t1 
LEFT OUTER JOIN Table1 t2 ON
    t1.variable_col = t2.variable_col 
    AND t1.timecomes_col < t2.timegoes_col AND
    t2.timegoes_col < DATETIME(t1.timecomes_col, '+6 minutes')

It might be helpful to add some sample data where this and other more traditional sql would fail to meet your needs. I assume there is a reason you are thinking of Window Functions to solve the issue, but given your sample data there doesn't appear to be a need for them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...