Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

sql - need to find start and end time of events which is still not active

I have a table with 3 coulmne timestamp eventid active

timestamp eventid active
2020-02-01 22:44:23 E1 true
2020-02-01 22:45:23 E1 false
2020-02-01 22:46:23 E1 true
2020-02-01 22:47:23 E1 false
2020-02-01 22:44:23 E2 true
2020-02-01 22:45:23 E2 false
2020-02-01 22:46:23 E2 true
question from:https://stackoverflow.com/questions/65545984/need-to-find-start-and-end-time-of-events-which-is-still-not-active

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

1 Reply

0 votes
by (71.8m points)
 ;WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY eventid ORDER BY timestamp DESC) AS rn
   FROM yourTable
)
SELECT min(yt.timestamp) as Start_Time, max(yt.timestamp) as End_Time,yt.eventid
FROM yourTable yt
inner join cte
On cte.eventid=yt.eventid
WHERE 
cte.rn = 1 and cte.active="false"
group by yt.eventid

In this way you will not see E2, because of it is still active.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...