久久久久久久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?什么時候不能投射為日期?)
主站蜘蛛池模板: 久久99精品久久久久久国产越南 | 免费久久网 | 一级片在线观看 | 伊色综合久久之综合久久 | 成人在线视频一区 | 婷婷午夜天| 日韩欧美在线观看一区 | 一区二区三区欧美 | 一区二区视频在线观看 | 欧美色图综合网 | 欧美成人免费在线 | 久久国| 色呦呦网站 | 国产成人免费视频网站视频社区 | 午夜日韩| 射久久 | 九九热精品视频 | 亚洲成网 | 久久久综合久久 | 在线免费黄色小视频 | 精品亚洲一区二区三区 | av一级久久 | 91精品国模一区二区三区 | 欧美视频三区 | 国产一区二区欧美 | 国产乱码精品一区二区三区中文 | 欧美高清视频一区 | 中文无码日韩欧 | 亚洲国产中文字幕 | 成人 在线 | 毛片综合 | caoporn地址| 成人一级视频在线观看 | 颜色网站在线观看 | 日本不卡在线视频 | 99久久99热这里只有精品 | 麻豆久久久久 | 成人高清在线 | 美女黄网 | 欧美日韩第一页 | 第一福利社区1024 |