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
172 views
in Technique[技术] by (71.8m points)

how to display different data sequentially date of status 1 first in sql server

how to display different data sequentially date of status 1 first

result like this image

Results

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

First creating the test data:

DECLARE @mydate TABLE
(
    date_time DATETIME,
    statusid int
)

INSERT INTO @mydate
( date_time, statusid )
VALUES
('02/25/2015 12:09:00', 0),
('02/25/2015 12:10:00', 0),
('02/25/2015 12:11:00', 0),
('02/25/2015 12:12:00', 1),
('02/25/2015 12:13:00', 1),
('02/25/2015 12:14:00', 0),
('02/25/2015 12:15:00', 0),
('02/25/2015 12:16:00', 1),
('02/25/2015 12:17:00', 1),
('02/25/2015 12:18:00', 1),
('02/25/2015 12:19:00', 1),
('02/25/2015 12:20:00', 0),
('02/25/2015 12:21:00', 0);

Lets find out what status is before each record

; WITH StatusRecs AS
(
    SELECT 
        m.date_time, 
        m.statusid, 
        LAG(m.statusid) OVER (ORDER BY date_time) AS prev_status
    FROM @mydate m
)

Now going to pull all of the status 1 records that are different from the prev status to find the beginning of each set, we are also going to pull the date of the next status = 1 record in next_start_date

,StartStatus AS
(
    SELECT 
        s.date_time, 
        s.statusid, 
        LEAD(s.date_time) OVER (ORDER BY s.date_time) AS next_start_date 
    FROM StatusRecs s
    WHERE s.statusid != ISNULL(s.prev_status, -1)
    AND s.statusid = 1
)

Now lets pull it all together to get the last 0 status record before the next status 1 record

,MyRecs AS
(
    SELECT * FROM StartStatus ss
    OUTER APPLY
    (
        SELECT TOP 1 sr.date_time AS date_time2, sr.statusid AS statusid2 
        FROM StatusRecs sr
        WHERE sr.date_time > ss.date_time 
        AND (sr.date_time < ss.next_start_date OR ss.next_start_date IS NULL)
        ORDER BY sr.date_time DESC
    ) m
)

Now we format and output the table

SELECT m.date_time, m.statusid, m.date_time2, m.statusid2, DATEDIFF(MINUTE, m.date_time, m.date_time2) AS duration  FROM MyRecs m

date_time   statusid    date_time2  statusid2   duration
2015-02-25 12:12:00.000 1   2015-02-25 12:15:00.000 0   3
2015-02-25 12:16:00.000 1   2015-02-25 12:21:00.000 0   5

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

...