久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將連續的非零行標記為不同的分區?

Tag consecutive non zero rows into distinct partitions?(將連續的非零行標記為不同的分區?)
本文介紹了將連續的非零行標記為不同的分區?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

假設我們有這個簡單的架構和數據:

Suppose we have this simple schema and data:

DROP TABLE #builds
CREATE TABLE #builds (
    Id INT IDENTITY(1,1) NOT NULL,
    StartTime INT,
    IsPassed BIT
)
INSERT INTO #builds (StartTime, IsPassed) VALUES
(1, 1),
(7, 1),
(10, 0),
(15, 1),
(21, 1),
(26, 0),
(34, 0),
(44, 0),
(51, 1),
(60, 1)

SELECT StartTime, IsPassed, NextStartTime,
    CASE IsPassed WHEN 1 THEN 0 ELSE NextStartTime - StartTime END Duration
FROM (
    SELECT  
        LEAD(StartTime) OVER (ORDER BY StartTime) NextStartTime,
        StartTime, IsPassed
    FROM #builds
) x
ORDER BY StartTime

它產生以下結果集:

StartTime   IsPassed    NextStartTime   Duration
1           1           7               0
7           1           10              0
10          0           15              5
15          1           21              0
21          1           26              0
26          0           34              8
34          0           44              10
44          0           51              7
51          1           60              0
60          1           NULL            0

我需要總結非零連續 Duration 值,并在批處理中第一行的 StartTime 顯示它們.IE.我需要解決這個問題:

I need to summarize the non zero consecutive Duration values and to show them at the StartTime of the first row in the batch. I.e. I need to get to this:

StartTime   Duration
10          5
26          25

我就是不知道該怎么做.

I just can't figure out how to do it.

PS:當然,真實表包含更多行.

PS: The real table contains many more rows, of course.

推薦答案

這是一個間隙和孤島問題,需要將 IsPassed 恒定的每個部分劃分到不同的組中.這可以通過計算整個表的 ROW_NUMBER() 與由 IsPassed 分區的差異來完成.然后,您可以 SUM 為每個組的 Duration 值,其中 IsPassed = False 并取 MIN(StartTime) 到給出組第一行的StartTime:

This is a gaps and islands problem, requiring partitioning each section where IsPassed is constant into a different group. That can be done by computing the difference between ROW_NUMBER() over the entire table against partitioned by IsPassed. You can then SUM the Duration Values for each group where IsPassed = False and take the MIN(StartTime) to give the StartTime of the first row of the group:

WITH CTE AS (
  SELECT StartTime, IsPassed,
         LEAD(StartTime) OVER (ORDER BY StartTime) AS NextStartTime
  FROM #builds
),
CTE2 AS (
  SELECT StartTime, IsPassed, NextStartTime,
         CASE IsPassed WHEN 1 THEN 0 ELSE NextStartTime - StartTime END Duration,
         ROW_NUMBER() OVER (ORDER BY StartTime) -
         ROW_NUMBER() OVER (PARTITION BY IsPassed ORDER BY StartTime) AS grp
  FROM CTE
)
SELECT MIN(StartTime) AS StartTime, SUM(Duration) AS Duration
FROM CTE2
WHERE IsPassed = 0
GROUP BY grp
ORDER BY MIN(StartTime)

輸出:

StartTime   Duration
10          5
26          25

dbfiddle 演示

這篇關于將連續的非零行標記為不同的分區?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 91av在线免费看 | 免费观看毛片 | 日韩高清国产一区在线 | 久久国产欧美日韩精品 | 人人天天操 | 国产黄色在线观看 | 天堂色综合 | 欧美日韩视频在线 | 欧产日产国产精品99 | 久久91av| 一区二区精品 | 福利视频三区 | 羞羞的视频免费在线观看 | 在线播放中文字幕 | 一区二区三区日韩 | 伊人精品| 国产成人精品高清久久 | 久久精品小视频 | 国产精品久久九九 | 人人精品| 亚洲精品第一页 | 男插女下体视频 | 成年人视频免费在线观看 | 亚洲综合久久精品 | 极品电影院 | 搞黄视频免费看 | 亚洲在线看| www.四虎.com | 国产精品一区二区在线 | 欧美一区二区三区精品 | 精品久久久久久久久久久久久久 | 免费在线观看h片 | 婷婷中文字幕 | 欧美亚洲激情 | 精品久久久一区 | 天天干天天操天天看 | 国产高清一区二区三区 | 天天综合国产 | 日韩成人在线播放 | www.日韩 | 久久亚洲精品国产精品紫薇 |