我需要 return 基于日期的连续数据集中重复值的最大计数
I need to return the Max count of repeating values in a consecutive data set based on dates
这是我的 table 的示例:
MID RecordDate Volume
5555 4/1/2017 1
5555 4/2/2017 2
5555 4/3/2017 2
5555 4/4/2017 2
5555 4/5/2017 3
5555 4/6/2017 3
5555 4/7/2017 3
5555 4/8/2017 3
5555 4/9/2017 3
5555 4/10/2017 3
5555 4/11/2017 3
5555 4/12/2017 3
5555 4/13/2017 3
5555 4/14/2017 3
5555 4/15/2017 3
5555 4/16/2017 3
5555 4/17/2017 3
5555 4/18/2017 3
5555 4/19/2017 3
5555 4/20/2017 3
5555 4/21/2017 3
5555 4/22/2017 3
5555 4/23/2017 3
5555 4/24/2017 3
5555 4/25/2017 3
5555 4/26/2017 2
5555 4/27/2017 2
5555 4/28/2017 2
5555 4/29/2017 2
5555 4/30/2017 2
我正在尝试 return 在定义的时间段内具有相同交易量的连续天数。在我的示例中,数据是从 2017 年 4 月 1 日到 2017 年 4 月 30 日,但实际上想将其调整为 30/40/60/90 天的任何日期范围..
我的预期结果与此类似:
+------+-------------+------------+---------+--------+
| MID | Start Date | End Date | Volume | Count |
+------+-------------+------------+---------+--------+
| 5555 | 4/5/2017 | 4/25/2017 | 3 | 21 |
+------+-------------+------------+---------+--------+
我为我的格式不当提前道歉。提前致谢!
所以这是该示例的解决方案代码,其他人可以帮助我为具有多个 MID 的 table 工作吗,我需要为每个 MID 提取最大的插值计数:
with cte as
(
Select top 1
md1.merrickid
, StartDate = convert(char(10),min(md1.RecordDate),120)
, EndDate = convert(char(10),max(md1.RecordDate),120)
, md1.EstGasVolMCF
, cnt = count(*)
From (
Select md.MerrickID
, md.RecordDate
, md.EstGasVolMCF
, grp = row_number() over (partition by md.merrickid order by md.RecordDate)
- row_number() over (partition by md.merrickid, md.EstGasVolMCF order by md.RecordDate)
From MeterDailyTb as md
Join CompletionTb as C
On c.MerrickID = md.MerrickID
Where md.RecordDate > '3/2/2017' --Parameter for the how far back you want to go
And c.DrillingTeamID in (24)--Drilling Team Selection Criteria
) as md1
--Where md1.merrickid = '105100'
group by md1.merrickid, md1.EstGasVolMCF, grp
order by cnt desc
)
Select *
From cte
Where cte.cnt > 30
这是一个gaps-and-islands type question, and can be handled using two row_number()
整理你的岛屿。
select
mid
, StartDate = min(RecordDate)
, EndDate = max(RecordDate)
, Volume
, cnt = count(*)
from (
select *
, grp = row_number() over (partition by mid order by RecordDate)
- row_number() over (partition by mid, volume order by RecordDate)
from t
) as s
group by mid, volume, grp
order by mid, StartDate
rextester 演示:http://rextester.com/JBFXNW15938
dbfiddle.uk 演示:http://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=82e7375b5e8d93386f362da547851de8
return秒:
+------+------------+------------+--------+-----+
| mid | StartDate | EndDate | Volume | cnt |
+------+------------+------------+--------+-----+
| 5555 | 2017-04-01 | 2017-04-01 | 1 | 1 |
| 5555 | 2017-04-02 | 2017-04-04 | 2 | 3 |
| 5555 | 2017-04-05 | 2017-04-25 | 3 | 21 |
| 5555 | 2017-04-26 | 2017-04-30 | 2 | 5 |
+------+------------+------------+--------+-----+
到return只有最高计数的一行,使用top 1
和order by cnt desc
。
select top 1
mid
, StartDate = convert(char(10),min(RecordDate),120)
, EndDate = convert(char(10),max(RecordDate),120)
, Volume
, cnt = count(*)
from (
select *
, grp = row_number() over (partition by mid order by RecordDate)
- row_number() over (partition by mid, volume order by RecordDate)
from t
) as s
group by mid, volume, grp
order by cnt desc
returns:
+------+------------+------------+--------+-----+
| mid | StartDate | EndDate | Volume | cnt |
+------+------------+------------+--------+-----+
| 5555 | 2017-04-05 | 2017-04-25 | 3 | 21 |
+------+------------+------------+--------+-----+
更新问题:
使用top with ties
with row_number()
with cte as
(
select top 1 with ties
md1.merrickid
, StartDate = convert(char(10),min(md1.RecordDate),120)
, EndDate = convert(char(10),max(md1.RecordDate),120)
, md1.EstGasVolMCF
, cnt = count(*)
From (
Select md.MerrickID
, md.RecordDate
, md.EstGasVolMCF
, grp = row_number() over (partition by md.merrickid order by md.RecordDate)
- row_number() over (partition by md.merrickid, md.EstGasVolMCF order by md.RecordDate)
From MeterDailyTb as md
Join CompletionTb as C
On c.MerrickID = md.MerrickID
Where md.RecordDate > '3/2/2017' --Parameter for the how far back you want to go
And c.DrillingTeamID in (24)--Drilling Team Selection Criteria
) as md1
--Where md1.merrickid = '105100'
group by md1.merrickid, md1.EstGasVolMCF, grp
order by row_number() over (partition by mid order by count(*) desc)
)
Select *
From cte
Where cte.cnt > 30
或row_number()
without top with ties
with cte as
(
select
md1.merrickid
, StartDate = convert(char(10),min(md1.RecordDate),120)
, EndDate = convert(char(10),max(md1.RecordDate),120)
, md1.EstGasVolMCF
, cnt = count(*)
, rn = row_number() over (partition by mid order by count(*) desc)
From (
Select md.MerrickID
, md.RecordDate
, md.EstGasVolMCF
, grp = row_number() over (partition by md.merrickid order by md.RecordDate)
- row_number() over (partition by md.merrickid, md.EstGasVolMCF order by md.RecordDate)
From MeterDailyTb as md
Join CompletionTb as C
On c.MerrickID = md.MerrickID
Where md.RecordDate > '3/2/2017' --Parameter for the how far back you want to go
And c.DrillingTeamID in (24)--Drilling Team Selection Criteria
) as md1
--Where md1.merrickid = '105100'
group by md1.merrickid, md1.EstGasVolMCF, grp
)
Select *
From cte
Where cte.cnt > 30
and cte.rn = 1
请尝试使用以下代码:
DECLARE @Record TABLE (MID INT,RecordDate DATE, Volume INT)
INSERT INTO @Record VALUES
(5555,'4/1/2017',1),
(5555,'4/2/2017',2),
(5555,'4/3/2017',2),
(5555,'4/4/2017',2),
(5555,'4/5/2017',3),
(5555,'4/6/2017',3),
(5555,'4/7/2017',3),
(5555,'4/8/2017',3),
(5555,'4/9/2017',3),
(5555,'4/10/2017',3),
(5555,'4/11/2017',3),
(5555,'4/12/2017',3),
(5555,'4/13/2017',3),
(5555,'4/14/2017',3),
(5555,'4/15/2017',3),
(5555,'4/16/2017',3),
(5555,'4/17/2017',3),
(5555,'4/18/2017',3),
(5555,'4/19/2017',3),
(5555,'4/20/2017',3),
(5555,'4/21/2017',3),
(5555,'4/22/2017',3),
(5555,'4/23/2017',3),
(5555,'4/24/2017',3),
(5555,'4/25/2017',3),
(5555,'4/26/2017',2),
(5555,'4/27/2017',2),
(5555,'4/28/2017',2),
(5555,'4/29/2017',2),
(5555,'4/30/2017',2)
SELECT
DISTINCT(Volume),
COUNT(*)AS [Count],
MID,
MIN(RecordDate) AS StartDate,
MAX(RecordDate) AS EndDate
FROM
@Record
GROUP BY
Volume,MID
这是我的 table 的示例:
MID RecordDate Volume
5555 4/1/2017 1
5555 4/2/2017 2
5555 4/3/2017 2
5555 4/4/2017 2
5555 4/5/2017 3
5555 4/6/2017 3
5555 4/7/2017 3
5555 4/8/2017 3
5555 4/9/2017 3
5555 4/10/2017 3
5555 4/11/2017 3
5555 4/12/2017 3
5555 4/13/2017 3
5555 4/14/2017 3
5555 4/15/2017 3
5555 4/16/2017 3
5555 4/17/2017 3
5555 4/18/2017 3
5555 4/19/2017 3
5555 4/20/2017 3
5555 4/21/2017 3
5555 4/22/2017 3
5555 4/23/2017 3
5555 4/24/2017 3
5555 4/25/2017 3
5555 4/26/2017 2
5555 4/27/2017 2
5555 4/28/2017 2
5555 4/29/2017 2
5555 4/30/2017 2
我正在尝试 return 在定义的时间段内具有相同交易量的连续天数。在我的示例中,数据是从 2017 年 4 月 1 日到 2017 年 4 月 30 日,但实际上想将其调整为 30/40/60/90 天的任何日期范围..
我的预期结果与此类似:
+------+-------------+------------+---------+--------+
| MID | Start Date | End Date | Volume | Count |
+------+-------------+------------+---------+--------+
| 5555 | 4/5/2017 | 4/25/2017 | 3 | 21 |
+------+-------------+------------+---------+--------+
我为我的格式不当提前道歉。提前致谢!
所以这是该示例的解决方案代码,其他人可以帮助我为具有多个 MID 的 table 工作吗,我需要为每个 MID 提取最大的插值计数:
with cte as
(
Select top 1
md1.merrickid
, StartDate = convert(char(10),min(md1.RecordDate),120)
, EndDate = convert(char(10),max(md1.RecordDate),120)
, md1.EstGasVolMCF
, cnt = count(*)
From (
Select md.MerrickID
, md.RecordDate
, md.EstGasVolMCF
, grp = row_number() over (partition by md.merrickid order by md.RecordDate)
- row_number() over (partition by md.merrickid, md.EstGasVolMCF order by md.RecordDate)
From MeterDailyTb as md
Join CompletionTb as C
On c.MerrickID = md.MerrickID
Where md.RecordDate > '3/2/2017' --Parameter for the how far back you want to go
And c.DrillingTeamID in (24)--Drilling Team Selection Criteria
) as md1
--Where md1.merrickid = '105100'
group by md1.merrickid, md1.EstGasVolMCF, grp
order by cnt desc
)
Select *
From cte
Where cte.cnt > 30
这是一个gaps-and-islands type question, and can be handled using two row_number()
整理你的岛屿。
select
mid
, StartDate = min(RecordDate)
, EndDate = max(RecordDate)
, Volume
, cnt = count(*)
from (
select *
, grp = row_number() over (partition by mid order by RecordDate)
- row_number() over (partition by mid, volume order by RecordDate)
from t
) as s
group by mid, volume, grp
order by mid, StartDate
rextester 演示:http://rextester.com/JBFXNW15938
dbfiddle.uk 演示:http://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=82e7375b5e8d93386f362da547851de8
return秒:
+------+------------+------------+--------+-----+
| mid | StartDate | EndDate | Volume | cnt |
+------+------------+------------+--------+-----+
| 5555 | 2017-04-01 | 2017-04-01 | 1 | 1 |
| 5555 | 2017-04-02 | 2017-04-04 | 2 | 3 |
| 5555 | 2017-04-05 | 2017-04-25 | 3 | 21 |
| 5555 | 2017-04-26 | 2017-04-30 | 2 | 5 |
+------+------------+------------+--------+-----+
到return只有最高计数的一行,使用top 1
和order by cnt desc
。
select top 1
mid
, StartDate = convert(char(10),min(RecordDate),120)
, EndDate = convert(char(10),max(RecordDate),120)
, Volume
, cnt = count(*)
from (
select *
, grp = row_number() over (partition by mid order by RecordDate)
- row_number() over (partition by mid, volume order by RecordDate)
from t
) as s
group by mid, volume, grp
order by cnt desc
returns:
+------+------------+------------+--------+-----+
| mid | StartDate | EndDate | Volume | cnt |
+------+------------+------------+--------+-----+
| 5555 | 2017-04-05 | 2017-04-25 | 3 | 21 |
+------+------------+------------+--------+-----+
更新问题:
使用top with ties
with row_number()
with cte as
(
select top 1 with ties
md1.merrickid
, StartDate = convert(char(10),min(md1.RecordDate),120)
, EndDate = convert(char(10),max(md1.RecordDate),120)
, md1.EstGasVolMCF
, cnt = count(*)
From (
Select md.MerrickID
, md.RecordDate
, md.EstGasVolMCF
, grp = row_number() over (partition by md.merrickid order by md.RecordDate)
- row_number() over (partition by md.merrickid, md.EstGasVolMCF order by md.RecordDate)
From MeterDailyTb as md
Join CompletionTb as C
On c.MerrickID = md.MerrickID
Where md.RecordDate > '3/2/2017' --Parameter for the how far back you want to go
And c.DrillingTeamID in (24)--Drilling Team Selection Criteria
) as md1
--Where md1.merrickid = '105100'
group by md1.merrickid, md1.EstGasVolMCF, grp
order by row_number() over (partition by mid order by count(*) desc)
)
Select *
From cte
Where cte.cnt > 30
或row_number()
without top with ties
with cte as
(
select
md1.merrickid
, StartDate = convert(char(10),min(md1.RecordDate),120)
, EndDate = convert(char(10),max(md1.RecordDate),120)
, md1.EstGasVolMCF
, cnt = count(*)
, rn = row_number() over (partition by mid order by count(*) desc)
From (
Select md.MerrickID
, md.RecordDate
, md.EstGasVolMCF
, grp = row_number() over (partition by md.merrickid order by md.RecordDate)
- row_number() over (partition by md.merrickid, md.EstGasVolMCF order by md.RecordDate)
From MeterDailyTb as md
Join CompletionTb as C
On c.MerrickID = md.MerrickID
Where md.RecordDate > '3/2/2017' --Parameter for the how far back you want to go
And c.DrillingTeamID in (24)--Drilling Team Selection Criteria
) as md1
--Where md1.merrickid = '105100'
group by md1.merrickid, md1.EstGasVolMCF, grp
)
Select *
From cte
Where cte.cnt > 30
and cte.rn = 1
请尝试使用以下代码:
DECLARE @Record TABLE (MID INT,RecordDate DATE, Volume INT)
INSERT INTO @Record VALUES
(5555,'4/1/2017',1),
(5555,'4/2/2017',2),
(5555,'4/3/2017',2),
(5555,'4/4/2017',2),
(5555,'4/5/2017',3),
(5555,'4/6/2017',3),
(5555,'4/7/2017',3),
(5555,'4/8/2017',3),
(5555,'4/9/2017',3),
(5555,'4/10/2017',3),
(5555,'4/11/2017',3),
(5555,'4/12/2017',3),
(5555,'4/13/2017',3),
(5555,'4/14/2017',3),
(5555,'4/15/2017',3),
(5555,'4/16/2017',3),
(5555,'4/17/2017',3),
(5555,'4/18/2017',3),
(5555,'4/19/2017',3),
(5555,'4/20/2017',3),
(5555,'4/21/2017',3),
(5555,'4/22/2017',3),
(5555,'4/23/2017',3),
(5555,'4/24/2017',3),
(5555,'4/25/2017',3),
(5555,'4/26/2017',2),
(5555,'4/27/2017',2),
(5555,'4/28/2017',2),
(5555,'4/29/2017',2),
(5555,'4/30/2017',2)
SELECT
DISTINCT(Volume),
COUNT(*)AS [Count],
MID,
MIN(RecordDate) AS StartDate,
MAX(RecordDate) AS EndDate
FROM
@Record
GROUP BY
Volume,MID