当两个或多个连续行具有相同状态时如何选择一行
Posted
技术标签:
【中文标题】当两个或多个连续行具有相同状态时如何选择一行【英文标题】:How to select one row when two or more consecutive rows have same statuses 【发布时间】:2020-08-15 13:25:07 【问题描述】:说明: 我有一个表格,它使用日期时间维护传感器及其状态(运行/停止)的数据。如下图所示:
我将这些数据转换成这样:
使用这个查询:
select * from (
Select A.TagName,
(select top 1 EventTime from PumpRunCounts_perf where status = 'Running' and EventTime<A.EventTime order by 1 desc) StartTime,
A.EventTime, A.Statuses
from (select * from PumpRunCounts_perf where status = 'Stopped')A
)A
但是当出现连续状态时,就会出现问题,如下所示
我想在连续状态中实现的是,当给出连续的“正在运行”状态时,选择最旧的状态,当有连续的“停止”状态时选择最新的状态。
像这样:
我已经使用游标来完成工作,但是超过 500 万行需要几天才能完成。不共享光标代码,为简单起见,如果需要,请告诉我。
请帮助我完成这项任务。谢谢
请查看示例表架构和数据
CREATE TABLE [dbo].[pumpruncounts_pref](
[ID] [nvarchar](36) NULL,
[EventTime] [datetime2](7) NULL,
[Tagname] [nvarchar](4000) NULL,
[Status] [nvarchar](4000) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'C6A41E1E-2C5B-4A7A-8858-67EBA622DC24', CAST(N'2020-07-22T23:06:00.0700000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'EDDE7B77-4A59-4BE0-95F5-31532BF2CBA1', CAST(N'2020-07-21T12:06:45.5640000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'B44CAC7C-B648-44BD-9BC1-B647CE448124', CAST(N'2020-07-23T19:10:50.3870000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'093C6C46-2A38-4A71-83B2-FDF849B06016', CAST(N'2020-07-18T05:54:52.7430000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'69A1610E-6859-4C56-845C-0A4EFF215A41', CAST(N'2020-07-20T04:59:45.2150000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'37DD901A-F2A0-4142-887D-D22CB38BB1F2', CAST(N'2020-07-20T03:19:40.7580000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'1D3F54F6-A129-43BB-B30B-4782AB587711', CAST(N'2020-07-24T03:39:44.4840000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'8A9C9058-4721-4C4F-A0D4-0B93B48AEC1A', CAST(N'2020-07-22T01:57:20.8030000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'A41E1295-C196-4E24-BDF4-62FC0B321BE3', CAST(N'2020-07-23T10:36:46.2140000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'7EAB2F61-EC61-448B-AA41-5C097C2F01EE', CAST(N'2020-07-20T13:41:35.4600000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'75746488-03B4-4639-B1AD-9882297B8FA6', CAST(N'2020-07-23T20:01:12.6110000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'3ADA8A20-0E79-4AAA-A7A7-FC17C7FC21E4', CAST(N'2020-07-19T04:12:21.9360000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'4CCEF052-46B1-4674-8278-C6649FD42C85', CAST(N'2020-07-23T10:10:33.3180000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'707EB5F2-CF3B-4699-9C59-C883615897C5', CAST(N'2020-07-21T21:54:10.1870000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'E7104F6D-82E3-49F5-B446-6A371A1E558A', CAST(N'2020-07-21T21:05:57.5550000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'23358E48-E947-4273-ACCC-E52A0A8DB137', CAST(N'2020-07-22T16:33:22.3440000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'274C023F-5E7B-4626-AD96-CA9BB42EE6A0', CAST(N'2020-07-22T03:34:21.3950000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'C58C6FB7-478E-4E32-AB08-535218AC3306', CAST(N'2020-07-24T04:12:08.0600000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'3F4E8D3C-0101-4A74-93CC-922F7700C83B', CAST(N'2020-07-20T19:04:40.4240000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'A1C363E2-3F14-4F9A-80B8-DC8B6634C2AB', CAST(N'2020-07-20T22:20:48.7130000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'95DC2DD7-CD27-4783-864D-4287A90DEDB3', CAST(N'2020-07-19T21:27:06.5570000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'9D3BAA7D-569D-414C-ADE0-F5263BACE1FF', CAST(N'2020-07-20T16:39:15.2270000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'B1C3AE46-3713-40A9-BA46-C3209EB7120C', CAST(N'2020-07-24T04:40:33.3250000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'DC1F0A0A-D767-494F-968A-07B6823CB26D', CAST(N'2020-07-20T20:14:50.2330000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'B497FA37-0E0F-4C19-9DF3-16735799ED02', CAST(N'2020-07-19T14:25:49.0880000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'CAAED751-4EE5-4FEF-98E2-0EEE8243CC1C', CAST(N'2020-07-22T04:06:56.9780000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'46258565-5C3F-45FB-AA36-78E3AAE0E88B', CAST(N'2020-07-23T03:43:54.3460000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'F7A1DB90-11D0-489F-A7BF-3D882153C6F3', CAST(N'2020-07-21T04:13:58.3370000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'38496DC3-BC8D-4D4C-9F0F-A0FC6285C009', CAST(N'2020-07-24T04:06:56.3970000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'3FE2CA7D-D059-403C-8BF2-9E2167B2D758', CAST(N'2020-07-23T17:26:22.8540000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'EB1591B1-73D8-4755-9B4C-0222ADA4BA2D', CAST(N'2020-07-18T05:59:27.6120000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'44743BDB-E084-4B5F-86A0-99812EC1452D', CAST(N'2020-07-19T04:08:14.9520000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'C9AC6855-1BDF-4B5C-9514-1CAE0D86E1D0', CAST(N'2020-07-22T20:51:39.3850000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'7E65B1CD-EE80-48C7-8DFD-F5A0F8C1D4E5', CAST(N'2020-07-18T16:44:55.2720000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'0CAFDFDF-CA04-4B8E-81FD-E54C96338120', CAST(N'2020-07-23T02:42:17.7990000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'DE7EA0C5-2EB9-4433-B99D-253711F0D990', CAST(N'2020-07-22T03:29:24.8290000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'DB459A4A-F03E-4911-9134-AB74C5763A29', CAST(N'2020-07-19T15:59:08.0170000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'C0AAABE7-D3C8-4E58-8501-44F2807992F0', CAST(N'2020-07-21T03:37:42.8400000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'F9E84C95-D1B8-42BE-8E51-A1E5BC63823C', CAST(N'2020-07-22T02:22:35.4930000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'3C12D9B6-38FC-4CD0-B668-EC583D072DC0', CAST(N'2020-07-21T02:30:25.0890000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'D8B005DF-3443-49BD-ACFD-803CFDDEF58D', CAST(N'2020-07-21T04:47:35.6360000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'3D6D5222-9B25-45C1-A73A-2E31F9DED4E3', CAST(N'2020-07-23T15:16:24.2680000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'F9EC2A51-0F1F-4BCB-8FAE-DE70B89BB590', CAST(N'2020-07-23T16:56:09.4620000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'433A4288-2FE3-4C7B-9205-16E98D1A376B', CAST(N'2020-07-19T14:47:55.5760000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'D05ED43A-244F-43B5-B778-38442CF2F6A4', CAST(N'2020-07-22T23:06:41.4140000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'D1399731-0F0F-4019-A8A5-12A7E2CD9D36', CAST(N'2020-07-21T04:18:24.7070000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'B8231929-2062-45A4-A7E6-939EF4E994F2', CAST(N'2020-07-20T04:29:50.5050000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'5D88B78F-6178-4B4E-8ACA-FD888E916DB5', CAST(N'2020-07-22T10:33:41.0020000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'4FB517BD-401C-451A-AE35-EEF80D5262AE', CAST(N'2020-07-22T04:01:47.3900000' AS DateTime2), N'PS44_P06.RunS', N'Running')
GO
INSERT [dbo].[pumpruncounts_pref] ([ID], [EventTime], [Tagname], [Status]) VALUES (N'3E673EB3-BC3D-40B7-9BDA-ABDF192CE012', CAST(N'2020-07-22T20:28:47.4780000' AS DateTime2), N'PS44_P06.RunS', N'Stopped')
GO
【问题讨论】:
【参考方案1】:您想要稍后停止的行而不是第一个,这对我来说似乎很奇怪。但这就是你在这里问的问题。
一种方法使用 lag()
和 lead()
仅保留已停止(或 NULL)之后的“正在运行”行和下一行为“正在运行”(或 NULL)的“已停止”行。
然后一个简单的lead()
和过滤器得到你想要的:
select tagname, eventtime as starttime, next_eventtime as endtime
from (select p.*,
lead(eventtime) over (partition by tagname order by eventtime) as next_eventtime
from (select p.*,
lag(status) over (partition by tagname order by eventtime) as prev_status,
lead(status) over (partition by tagname order by eventtime) as next_status
from pumpruncounts_pref p
) p
where ( status = 'Running' and (prev_status is null or prev_status = 'Stopped') ) or
( status = 'Stopped' and (next_status is null or next_status = 'Running') )
) p
where status = 'Running';
Here 是一个 dbfiddle。
如果您真的希望 first 按顺序停止(这对我来说更有意义),那么您可以调整停止的逻辑以模仿开始(即查看前一行而不是下一个)。
为了提高性能,请在(tagname, eventtime, status)
上添加索引。
【讨论】:
感谢@Gordon 的帮助。我已经在完整数据上对其进行了测试,它返回了正确的结果。查询需要大约 20 分钟才能在 500 万行上执行。如果可以降低执行时间,您能否提供任何提示?谢谢。 @Miru 。 . .这似乎是一个很长的时间。我建议在答案中使用索引,但您的系统可能正在发生其他事情。特别是,pumpruncounts_pref
可能是一个非常复杂的查询视图。【参考方案2】:
或者用start和stop、前任和后继来标识start、running和end;然后每次遇到“真实”开始时添加 1 的运行总和,使用该运行总和作为会话标识符,最后按标签名称和会话 ID 分组。
您在打开的公用表表达式中的输入,就在 WITH
关键字之后,
然后是两个后续 CTE-s - 显示中间结果 - 最后是 GROUP BY 查询。
WITH
pumpruncounts_pref(id,eventtime,tagname,status) AS (
SELECT 'C9AC6855-1BDF-4B5C-9514-1CAE0D86E1D0', TIMESTAMP '2020-07-22T20:51:39.3850000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT '7E65B1CD-EE80-48C7-8DFD-F5A0F8C1D4E5', TIMESTAMP '2020-07-18T16:44:55.2720000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT '0CAFDFDF-CA04-4B8E-81FD-E54C96338120', TIMESTAMP '2020-07-23T02:42:17.7990000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT 'DE7EA0C5-2EB9-4433-B99D-253711F0D990', TIMESTAMP '2020-07-22T03:29:24.8290000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT 'DB459A4A-F03E-4911-9134-AB74C5763A29', TIMESTAMP '2020-07-19T15:59:08.0170000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT 'C0AAABE7-D3C8-4E58-8501-44F2807992F0', TIMESTAMP '2020-07-21T03:37:42.8400000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT 'F9E84C95-D1B8-42BE-8E51-A1E5BC63823C', TIMESTAMP '2020-07-22T02:22:35.4930000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT '3C12D9B6-38FC-4CD0-B668-EC583D072DC0', TIMESTAMP '2020-07-21T02:30:25.0890000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT 'D8B005DF-3443-49BD-ACFD-803CFDDEF58D', TIMESTAMP '2020-07-21T04:47:35.6360000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT '3D6D5222-9B25-45C1-A73A-2E31F9DED4E3', TIMESTAMP '2020-07-23T15:16:24.2680000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT 'F9EC2A51-0F1F-4BCB-8FAE-DE70B89BB590', TIMESTAMP '2020-07-23T16:56:09.4620000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT '433A4288-2FE3-4C7B-9205-16E98D1A376B', TIMESTAMP '2020-07-19T14:47:55.5760000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT 'D05ED43A-244F-43B5-B778-38442CF2F6A4', TIMESTAMP '2020-07-22T23:06:41.4140000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT 'D1399731-0F0F-4019-A8A5-12A7E2CD9D36', TIMESTAMP '2020-07-21T04:18:24.7070000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT 'B8231929-2062-45A4-A7E6-939EF4E994F2', TIMESTAMP '2020-07-20T04:29:50.5050000', 'PS44_P06.RunS', 'Stopped'
UNION ALL SELECT '5D88B78F-6178-4B4E-8ACA-FD888E916DB5', TIMESTAMP '2020-07-22T10:33:41.0020000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT '4FB517BD-401C-451A-AE35-EEF80D5262AE', TIMESTAMP '2020-07-22T04:01:47.3900000', 'PS44_P06.RunS', 'Running'
UNION ALL SELECT '3E673EB3-BC3D-40B7-9BDA-ABDF192CE012', TIMESTAMP '2020-07-22T20:28:47.4780000', 'PS44_P06.RunS', 'Stopped'
)
,
with_chg AS (
SELECT
eventtime
, tagname
, status
, CASE
WHEN ISNULL(LAG(status) OVER(PARTITION BY tagname ORDER BY eventtime),'Stopped')='Stopped'
AND status='Running'
THEN 'Start'
WHEN ISNULL(LEAD(status) OVER(PARTITION BY tagname ORDER BY eventtime),'Running')='Running'
AND status='Stopped'
THEN 'Stop'
ELSE 'Running'
END AS newstat
FROM pumpruncounts_pref
)
-- out eventtime | tagname | status | newstat
-- out -------------------------+---------------+---------+---------
-- out 2020-07-18 16:44:55.272 | PS44_P06.RunS | Stopped | Stop
-- out 2020-07-19 14:47:55.576 | PS44_P06.RunS | Running | Start
-- out 2020-07-19 15:59:08.017 | PS44_P06.RunS | Running | Running
-- out 2020-07-20 04:29:50.505 | PS44_P06.RunS | Stopped | Stop
-- out 2020-07-21 02:30:25.089 | PS44_P06.RunS | Running | Start
-- out 2020-07-21 03:37:42.84 | PS44_P06.RunS | Running | Running
-- out 2020-07-21 04:18:24.707 | PS44_P06.RunS | Stopped | Stop
-- out 2020-07-21 04:47:35.636 | PS44_P06.RunS | Running | Start
-- out 2020-07-22 02:22:35.493 | PS44_P06.RunS | Running | Running
-- out 2020-07-22 03:29:24.829 | PS44_P06.RunS | Running | Running
-- out 2020-07-22 04:01:47.39 | PS44_P06.RunS | Running | Running
-- out 2020-07-22 10:33:41.002 | PS44_P06.RunS | Running | Running
-- out 2020-07-22 20:28:47.478 | PS44_P06.RunS | Stopped | Stop
-- out 2020-07-22 20:51:39.385 | PS44_P06.RunS | Running | Start
-- out 2020-07-22 23:06:41.414 | PS44_P06.RunS | Stopped | Running
-- out 2020-07-23 02:42:17.799 | PS44_P06.RunS | Stopped | Running
-- out 2020-07-23 15:16:24.268 | PS44_P06.RunS | Stopped | Running
-- out 2020-07-23 16:56:09.462 | PS44_P06.RunS | Stopped | Stop
-- out (18 rows)
-- out
-- out Time: First fetch (18 rows): 24.252 ms. All rows formatted: 24.307 ms
,
with_session AS (
SELECT
eventtime
, tagname
, status
, newstat
, SUM(
CASE newstat
WHEN 'Start' THEN 1
ELSE 0
END
) OVER (PARTITION BY tagname ORDER BY eventtime) AS session_id
FROM with_chg
)
-- out eventtime | tagname | status | newstat | session_id
-- out -------------------------+---------------+---------+---------+------------
-- out 2020-07-18 16:44:55.272 | PS44_P06.RunS | Stopped | Stop | 0
-- out 2020-07-19 14:47:55.576 | PS44_P06.RunS | Running | Start | 1
-- out 2020-07-19 15:59:08.017 | PS44_P06.RunS | Running | Running | 1
-- out 2020-07-20 04:29:50.505 | PS44_P06.RunS | Stopped | Stop | 1
-- out 2020-07-21 02:30:25.089 | PS44_P06.RunS | Running | Start | 2
-- out 2020-07-21 03:37:42.84 | PS44_P06.RunS | Running | Running | 2
-- out 2020-07-21 04:18:24.707 | PS44_P06.RunS | Stopped | Stop | 2
-- out 2020-07-21 04:47:35.636 | PS44_P06.RunS | Running | Start | 3
-- out 2020-07-22 02:22:35.493 | PS44_P06.RunS | Running | Running | 3
-- out 2020-07-22 03:29:24.829 | PS44_P06.RunS | Running | Running | 3
-- out 2020-07-22 04:01:47.39 | PS44_P06.RunS | Running | Running | 3
-- out 2020-07-22 10:33:41.002 | PS44_P06.RunS | Running | Running | 3
-- out 2020-07-22 20:28:47.478 | PS44_P06.RunS | Stopped | Stop | 3
-- out 2020-07-22 20:51:39.385 | PS44_P06.RunS | Running | Start | 4
-- out 2020-07-22 23:06:41.414 | PS44_P06.RunS | Stopped | Running | 4
-- out 2020-07-23 02:42:17.799 | PS44_P06.RunS | Stopped | Running | 4
-- out 2020-07-23 15:16:24.268 | PS44_P06.RunS | Stopped | Running | 4
-- out 2020-07-23 16:56:09.462 | PS44_P06.RunS | Stopped | Stop | 4
SELECT
tagname
, session_id
, MIN(eventtime) AS start_ts
, MAX(eventtime) AS end_ts
FROM with_session
GROUP BY
tagname
, session_id
ORDER BY
tagname
, session_id
-- out tagname | session_id | start_ts | end_ts
-- out ---------------+------------+-------------------------+-------------------------
-- out PS44_P06.RunS | 0 | 2020-07-18 16:44:55.272 | 2020-07-18 16:44:55.272
-- out PS44_P06.RunS | 1 | 2020-07-19 14:47:55.576 | 2020-07-20 04:29:50.505
-- out PS44_P06.RunS | 2 | 2020-07-21 02:30:25.089 | 2020-07-21 04:18:24.707
-- out PS44_P06.RunS | 3 | 2020-07-21 04:47:35.636 | 2020-07-22 20:28:47.478
-- out PS44_P06.RunS | 4 | 2020-07-22 20:51:39.385 | 2020-07-23 16:56:09.462
【讨论】:
感谢 marcothesane。我会去戈登的回答,这对我来说很容易处理。感谢您的帮助。以上是关于当两个或多个连续行具有相同状态时如何选择一行的主要内容,如果未能解决你的问题,请参考以下文章
当该行的 clos 与上一行具有重复值时,如何进行 sql Select 查询,以便在后续行中将某些列留空?