[英]select records with a column with specific value on specific date sql server 2000
[英]SQL Server, Getting records from random date where not started with specific column value
我想从我的一个表中获取数据,该表具有 EventTime 列,该列保留 DateTime 和 EventType 列,该列的值为 1 和 5。
结果数据应按 NetworkNode 返回两个事件类型之间的持续时间总和,还应按 NetworkNode 返回从 1 到 5 组的 EventType 更改次数。
表结构如下,不可更改:
CREATE TABLE [dbo].[Events]
(
[EventID] [INT] NOT NULL,
[EventTime] [DATETIME] NULL,
[NetworkNode] [INT] NULL,
[EventType] [SMALLINT] NULL,
[Message] [NVARCHAR](MAX) NULL,
[NetObjectType] [VARCHAR](10) NULL,
CONSTRAINT [PK_Events]
PRIMARY KEY NONCLUSTERED ([EventID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
插入表中的样本数据如下:
1 值表示连接超时,5 值表示连接再次响应。
因此,它可能有不止一行具有 1 个值(EventType),直到出现 5 个值。
获取数据的过滤器是EventTime。 例如,我必须从 30 天前获取数据,因此当我尝试按当前日期前 30 天进行过滤时,结果的第一行可能会返回 5 作为 EventType 值,并且它使持续时间为负。
有什么方法可以删除EventType值等于5的查询结果的第一行吗?
仅供了解:
此外,如果在具有 (EventType = 5) 的记录之前连续有多个记录 (EventType = 1),则必须忽略除最后一条 (EventType = 1) 之外的所有重复记录,因为在 (EventType = 1) 之间获得正确的持续时间和(事件类型 = 5)。
是否可以仅使用 SQL 服务器查询? 或者它也需要使用某种编程语言? 如C#和LINQ等。
我将您的要求理解为间隙和孤岛问题的一个变体:基本上,您希望将具有相同网络节点和状态的连续记录组合在一起。
这是一种方法,它将为您提供具有相同NetworkNode
和EventType
的每个记录系列的第一个和最后一个EventID
,以及该系列中的事件数量、开始和结束EventTime
以及它们的差异(以分钟为单位)。
select
NetworkNode,
EventType,
min(EventID) FirstEventID,
max(EventID) LastEventID,
count(*) EventCount
min(EventTime) FirstEventTime,
max(EventTime) LastEventTime,
datediff(minute, min(EventTime), max(EventTime)) Duration
from (
select
e.*,
row_number() over(partition by NetworkNode order by EventTime) rn1,
row_number() over(partition by NetworkNode, EventType order by EventTime) rn2
from events e
) e
group by
NetworkNode,
EventType,
rn1 - rn2
@Larnu 抱歉我的问题中格式错误。
这是表中插入数据的 10 行:
EventID EventTime NetworkNode EventType
1 2019-11-10 15:34:00.000 3 1
2 2019-11-10 15:46:00.000 3 5
3 2019-11-10 16:08:00.000 5 1
4 2019-11-10 16:28:00.000 5 5
5 2019-11-10 16:36:00.000 5 1
6 2019-11-10 16:46:00.000 5 5
7 2019-11-11 10:18:00.000 9 1
8 2019-11-11 10:26:00.000 9 5
9 2019-11-11 13:45:00.000 9 1
10 2019-11-11 13:51:00.000 9 5
我试图通过下面的查询获得结果并且它工作正常,但是当我想按 NetworkNode 对行进行分组时,会出现如下错误:
列“Events.EventID”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
select
t1.EventID,
t1.EventTime,
t2.EventTime,
t1.NetworkNode,
t1.EventType,
t2.EventType,
DATEDIFF(minute , t1.EventTime, t2.EventTime) as Duration
from
(
select
*,
ROW_NUMBER() OVER(
Order by
eventid) as CNumber
from
Events
where
EventTime >= DATEADD(day, - 30, GETDATE())
and NetObjectType = 'N'
And EventType = 1
)
as t1
inner join
(
select
*,
ROW_NUMBER() OVER( Order by eventid) as CNumber
from
Events
where
EventTime >= DATEADD(day, - 30, GETDATE())
and NetObjectType = 'N'
And EventType = 5
)
as t2
on t1.CNumber = t2.CNumber AND t1.NetworkNode = t2.NetworkNode
非常感谢@GMB,这里是查询及其结果,它太接近我的预期,但有一些问题,我知道这是因为我错误地转移了这个概念。
询问 :
select
NetworkNode,
EventType,
min(EventID) FirstEventID,
max(EventID) LastEventID,
count(*) EventCount
min(EventTime) FirstEventTime,
max(EventTime) LastEventTime,
datediff(minute, min(EventTime), max(EventTime)) Duration
from (
select
e.*,
row_number() over(partition by NetworkNode order by EventTime) rn1,
row_number() over(partition by NetworkNode, EventType order by EventTime) rn2
from events e
) e
group by
NetworkNode,
EventType,
rn1 - rn2
结果 :
Node Event FirstEvent LastEvent EventCount FirstEventTime LastEventTime Duration
24 1 13191615 13191615 1 2019-11-16 22:12:14.000 2019-11-16 22:12:14.000 0
24 5 13191653 13191653 1 2019-11-16 22:14:35.000 2019-11-16 22:14:35.000 0
25 1 12072600 12072600 1 2019-10-22 12:53:34.000 2019-10-22 12:53:34.000 0
25 5 12074438 12074438 1 2019-10-22 14:19:48.000 2019-10-22 14:19:48.000 0
26 1 12740974 12741155 4 2019-11-02 12:47:41.000 2019-11-02 12:47:53.000 12
26 1 12741302 13039438 2 2019-11-02 12:48:07.000 2019-11-10 15:03:00.000 699293
26 5 12741301 12741301 1 2019-11-02 12:48:07.000 2019-11-02 12:48:07.000 0
26 5 13039471 13039471 1 2019-11-10 15:05:07.000 2019-11-10 15:05:07.000 0
预期结果 :
NetworkNode | Count of Happening | Sum of Duration for all Happenings (Min or Sec)
24 | 10 | 50
25 | 14 | 46
26 | 29 | 128
27 | 3 | 7
28 | 21 | 39
29 | 75 | 481
***** 发生的含义**:每个网络节点的事件类型 1 到事件类型 5 之间的持续时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.