Sql 我需要返回基于日期的连续数据集中重复值的最大计数
以下是我的表格示例:Sql 我需要返回基于日期的连续数据集中重复值的最大计数,sql,sql-server,tsql,gaps-and-islands,Sql,Sql Server,Tsql,Gaps And Islands,以下是我的表格示例: 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
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
我试图返回定义时间段内具有相同容量的连续天数计数。在我的示例中,数据是从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的表执行此操作,其中我需要提取每个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
这是一个类型问题,可以使用两个s来整理您的岛屿
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演示:
dbfiddle.uk演示:
返回:
+------+------------+------------+--------+-----+
| 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 |
+------+------------+------------+--------+-----+
要仅返回计数最高的一行,请使用
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
返回:
+------+------------+------------+--------+-----+
| mid | StartDate | EndDate | Volume | cnt |
+------+------------+------------+--------+-----+
| 5555 | 2017-04-05 | 2017-04-25 | 3 | 21 |
+------+------------+------------+--------+-----+
有关最新问题: 配合使用 还是没有
请尝试使用以下代码:
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
您使用的是哪种DBMS?将表和数据作为文本Microsoft SQL Server Hi Zim发布。雷克斯测试仪今天能为您工作吗。我无法打开它。@JuanCarlosOropeza在rextester上对我没有问题。不过我会加一把db提琴。奇怪。DBFIDLE open ok,但rextester没有:(…但不仅仅是您的,我今天尝试创建了几次都没有成功。看起来我的办公室互联网有问题,我远程访问我的家用电脑,并且在那里工作正常。顺便说一句,我可能想要
订单计数(*)DESC
@JuanCarlosOropeza啊,对。更新为top 1
cnt DESC
感谢您花时间发布此可能的解决方案。唯一的问题是,这没有考虑到我只想根据日期范围计算相同且连续的值。间隙和孤岛解决方案正是相同的我需要的解决方案。
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