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

計算連續天數 SQL Server

Counting Consecutive days SQL Server(計算連續天數 SQL Server)
本文介紹了計算連續天數 SQL Server的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望計算每個人的連續日法術.

I am looking to count consecutive day spells for each individual person.

我的桌子:

CREATE TABLE Absence(
Date Date,
Code varchar(10),
Name varchar(10),
Type varchar(10)
);

INSERT INTO Absence (Date, Code, Name, Type)
VALUES ('01-10-18', 'S', 'Sam', 'Sick'),
('01-11-18','S', 'Sam', 'Sick'),
('01-12-18','S', 'Sam', 'Sick'),
('01-21-18','S', 'Sam', 'Sick'),
('01-26-18','S', 'Sam', 'Sick'),
('01-27-18','S', 'Sam', 'Sick'),
('02-12-18','S', 'Sam', 'Holiday'),
('02-13-18','S', 'Sam', 'Holiday'),
('02-18-18','S', 'Sam', 'Holiday'),
('02-25-18','S', 'Sam', 'Holiday'),
('02-10-18','S', 'Sam', 'Holiday'),
('02-13-18','F', 'Fred', 'Sick'),
('02-14-18','F', 'Fred', 'Sick'),
('02-17-18','F', 'Fred', 'Sick'),
('02-25-18','F', 'Fred', 'Sick'),
('02-28-18','F', 'Fred', 'Sick');

這是我目前擁有的代碼:

This is the code i currently have:

WITH CTE AS
(
SELECT 
Date,
Name, 
Type
,GroupingSet = DATEADD(DAY, ROW_NUMBER() OVER 
(PARTITION BY [Name], [Type] ORDER BY [Date]), [Date])
FROM Absence
)
SELECT 
    Name,
    StartDate = MIN(Date),
    EndDate = MAX(Date),
    Result = COUNT(Name),
    min(Type) AS [Type]
    FROM CTE

   GROUP BY Name, GroupingSet
    -- HAVING COUNT(NULLIF(Code, 0)) > 1
   ORDER BY Name, StartDate

產生的結果:

| Name |  StartDate |    EndDate | Result |    Type |
|------|------------|------------|--------|---------|
| Fred | 2018-02-13 | 2018-02-13 |      1 |    Sick |
| Fred | 2018-02-14 | 2018-02-14 |      1 |    Sick |
| Fred | 2018-02-17 | 2018-02-17 |      1 |    Sick |
| Fred | 2018-02-25 | 2018-02-25 |      1 |    Sick |
| Fred | 2018-02-26 | 2018-02-28 |      1 |    Sick |
|  Sam | 2018-01-10 | 2018-01-10 |      1 |    Sick |
|  Sam | 2018-01-11 | 2018-01-11 |      1 |    Sick |
|  Sam | 2018-01-12 | 2018-01-12 |      1 |    Sick |
|  Sam | 2018-01-21 | 2018-01-21 |      1 |    Sick |
|  Sam | 2018-01-26 | 2018-01-26 |      1 |    Sick |
|  Sam | 2018-01-27 | 2018-01-27 |      1 |    Sick |
|  Sam | 2018-02-10 | 2018-02-10 |      1 | Holiday |
|  Sam | 2018-02-12 | 2018-02-12 |      1 | Holiday |
|  Sam | 2018-02-13 | 2018-02-13 |      1 | Holiday |
|  Sam | 2018-02-18 | 2018-02-18 |      1 | Holiday |
|  Sam | 2018-02-25 | 2018-02-25 |      1 | Holiday |

我正在尋找這樣的結果集:

Where as i am looking for a result set like this:

| Name |       Date | Result  |    Type |
|------|------------|---------|---------|
| Fred | 2018-02-13 |       2 |    Sick |
|  Sam | 2018-01-27 |       2 |    Sick |
|  Sam | 2018-02-10 |       1 | Holiday |

我需要計算連續超過 1 天的連續天數.然后把這個作為一個人有多少連續法術的總數.例如在那段時間里,弗雷德連續兩次生病.如果有人周五和周一休息,我也需要這個來覆蓋,這應該算作連續的咒語.

I need to count the consecutive days where there is more then 1 day in a row. And then have this as a total of how many consecutive spells someone had. e.g. fred had 2 consecutive sick spells during that time period. I also need this to cover if someone had a friday and a monday off, this should count as a consecutive spell.

我對如何到達那里有點迷茫.任何幫助將不勝感激.

Im a bit lost as to how to get there. Any help would be appreciate.

請參閱:http://sqlfiddle.com/#!18/88612/16

推薦答案

您可以使用以下方法獲取缺勤時間:

You can get the periods of absences using:

select name, min(date), max(date), count(*) as numdays, type
from (select a.*,
             row_number() over (partition by name, type order by date) as seqnum_ct
      from absence a
     ) a
group by name, type, dateadd(day, -seqnum_ct, date);

這里是一個 SQL Fiddle.

Here is a SQL Fiddle for this.

你可以添加有count(*)>1 獲得一天或更長時間的經期.這似乎很有用.我不明白最終的輸出是什么.描述對我來說沒有意義.

You can add having count(*) > 1 to get periods with one day or more. This seems useful. I don't understand what the ultimate output is. The description just doesn't make sense to me.

如果您想要 2 天或更多天的缺勤次數,請將其用作子查詢/CTE:

If you want the number of absences that are 2 or more days, then use this as a subquery/CTE:

select name, count(*), type
from (select name, min(date) as mindate, max(date) as maxdate, count(*) as numdays, type
      from (select a.*,
                   row_number() over (partition by name, type order by date) as seqnum_ct
            from absence a
           ) a
      group by name, type, dateadd(day, -seqnum_ct, date)
     ) b
where numdays > 1
group by name, type;

這篇關于計算連續天數 SQL Server的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产日韩欧美中文 | 在线播放中文字幕 | 日韩精品人成在线播放 | 日韩精品成人av | 隔壁老王国产在线精品 | 久久精品视频99 | 福利视频一区 | 国产精品久久久久久久久久久久久 | 男女羞羞视频在线 | 亚洲国产专区 | 久久精品亚洲精品国产欧美 | 久热中文字幕 | 一区二区三区免费看 | 日韩精品影院 | 成人免费精品视频 | 白浆在线 | 亚洲精品久久久一区二区三区 | 国产不卡在线观看 | 色婷婷综合成人av | 亚洲美女在线一区 | 国产精品久久久久久久久久久免费看 | 日韩中文一区 | 国产成人免费视频网站高清观看视频 | 瑞克和莫蒂第五季在线观看 | 国产999精品久久久影片官网 | 老司机深夜福利网站 | 福利在线看| 一级黄色片在线看 | 亚洲国产成人精品女人久久久 | 人人澡人人爱 | www国产成人免费观看视频,深夜成人网 | 久久成人精品 | 国产成人艳妇aa视频在线 | 中文字幕不卡一区 | 中文字幕av在线 | 精品日韩电影 | www.jizzjizz| 久久久福利 | av大全在线| 亚洲精品在线视频 | xx视频在线观看 |