gaps-and-islands 相关问题

差距和岛屿问题涉及在一系列数字或日期中找到一系列缺失值(间隙)或一系列连续值(岛)。

如何按日期查找一行中连续出现的数据?

我想要解决的问题是我需要知道 EmployeeId + TenantId (1A) 在连续运行中出现了多少次。我真的只关心它是否大于 1。 我的应用程序在业务上运行...

回答 1 投票 0

SQL 查询计数仅获取最新记录计数

我有包含 userID、FY、clientID 的表 我想要一个 SQL 语句来为我提供用户与客户端的最新交互计数。 示例:在下表中,我的用户 1 进行了交互...

回答 1 投票 0

在 CTE 中以日期为键插入“缺失记录”

我有一个具有以下结构的 CTE 项目、日期、数量、额外 AAAA 2015年7月1日 100 20 AAAA 04-07-2015 100 13 AAAA 2015年7月7日 100 16 AAAA 09-07-

回答 2 投票 0

Postgresql 问题

我有一张这样的桌子: 名称 使用时间 阿斯达 10 15:00 BSF 15 15:00 阿斯德 20 14:55 BSF 0 14:55 我需要进行一个返回如下值的查询: 结果...

回答 3 投票 0

基于具有多个 dateFrom 和 DateTo 列的单独表构建时间线?

我有两个表,tblPrice 和 tblDiscount,每个表都有其值,其中包含 liveFrom 和 liveUntil 列(加上一些其他列,但我将这些列留出以尝试使其更简单)...

回答 1 投票 0

如何在 SQL 中使用窗口函数创建开始和结束日期存储桶

我有一个时间戳列表(WTIME)、与该时间戳相关的一些值(VAL)和一个类别(WTYPE)。每个时间戳只会有一个条目。我需要转换数据,以便我...

回答 1 投票 0

SQL 差距和孤岛

当同一类型的承诺之间存在差距时,事实应包含多个记录,显示每个连续承诺的准确开始和结束日期。这方面的一个例子是...

回答 2 投票 0

计算 sql 和/或 pandas 中的连续出现次数

我想查看每个学生连续缺席了多少次。我有一个名为 class_absenses 的表,其中包含以下字段: id, student_id, present (0代表缺席,1代表现在)...

回答 3 投票 0

将开始日期和结束日期拆分为开始日期/月的第一天和结束日期/月的最后一天

开始日期 结束日期 2018-02-21 2018-03-21 2018-03-21 2018-04-21 2018-04-21 2018-05-21 2018-05-21 2018-06-21 我想把它变成: 开始日期 结束日期 2018-02-21 2018-02-28 2018-...

回答 0 投票 0

按时间顺序计算连续的列值并在另一个表中更新结果

餐桌顾客 ID 行动 日期 时间 10 交易 26.04.2023 15:00 10 见面 25.04.2023 14:15 10 称呼 24.04.2023 13:00 10 等待 23.04.2023 12:00 201 等待 15.04.2023 14:30 201 等待 15.04.2023 13:30 20...

回答 1 投票 0

如何写“连胜”查询?

我有一个名为 MyDates 的表,其中包含列 Id (INT) 和 MyDate (DATE)。 MyDates 可以包含具有相同日期的多行。 我想统计连续工作日的天数,从今天开始和

回答 2 投票 0

输入末尾的 Postgres Sql 语法错误

创建表 hall_events ( hall_id 整数, start_date 日期, end_date 日期 ); 从 hall_events 中删除; 插入 hall_events 值 (1,'2023-01-13','2023-01-14') ,(1,'2023-01-14','2023-01-17')...

回答 2 投票 0

根据行的顺序获取开始和结束日期时间

我在下表中有示例数据。 约会时间 信息 13/07/2022 8:59:09 普通的 13/07/2022 8:59:10 普通的 13/07/2022 8:59:11 普通的 13/07/2022 8:59:12 警告 13/07/2022 8:59:13 警告...

回答 2 投票 0

ROW_NUMBER 或其他序列取决于日期 (SQL)

我在使用 row_number 创建序列时遇到问题,但仍然无法处理。 我有一张桌子 公元前 IO 日期 1a 11 2022-01-01 1a 11 2022-01-02 1a 12 2022-01-03 1a 11 2022-01-04 当我使用 sim 时...

回答 2 投票 0

条件前置值

我的样本数据是这样的 降低 表如果存在#temp 选择 *进入#temp 从 ( 价值观 ('id100', 'status1', 1), ('id100', 'status2', 2), ('id100', 'status1', 3),...

回答 3 投票 0

基于条件的SQL排名

我正在尝试添加另一个名为 session_id 的列。我想按照date_time之间的时间差超过30分钟的条件来进行排名,那就算作

回答 3 投票 0

每组填写行以进行旋转

我有一个名为 LOGS 的 Oracle 18c 表: 创建表日志(id 号,log_tags varchar2(4000)); 插入日志 (id, log_tags) 值 ( 1,' 我有一个名为 LOGS 的 Oracle 18c 表: create table logs (id number, log_tags varchar2(4000)); insert into logs (id, log_tags) values ( 1,'<Event time="Sat Apr 15 1:13:17.750" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values ( 2,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values ( 3,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values ( 4,'</Event>'); insert into logs (id, log_tags) values ( 5,'<Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" '); insert into logs (id, log_tags) values ( 6,null); insert into logs (id, log_tags) values ( 7,'</Event>'); insert into logs (id, log_tags) values ( 8,'<Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" '); insert into logs (id, log_tags) values ( 9,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (10,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (11,' Number of features returned: 100'); insert into logs (id, log_tags) values (12,'</Event>'); insert into logs (id, log_tags) values (13,'<Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (14,null); insert into logs (id, log_tags) values (15,'</Event>'); insert into logs (id, log_tags) values (16,'<Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (17,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (18,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (19,'</Event>'); insert into logs (id, log_tags) values (20,'<Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" '); insert into logs (id, log_tags) values (21,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (22,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (23,' Number of features returned: 100'); insert into logs (id, log_tags) values (24,'</Event>'); insert into logs (id, log_tags) values (25,'<Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" '); insert into logs (id, log_tags) values (26,null); insert into logs (id, log_tags) values (27,'</Event>'); insert into logs (id, log_tags) values (28,'<Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (29,null); insert into logs (id, log_tags) values (30,'</Event>'); insert into logs (id, log_tags) values (31,'<Event time="Sat Apr 15 1:13:17.744" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (32,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (33,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (34,'</Event>'); insert into logs (id, log_tags) values (35,'<Event time="Sat Apr 15 1:13:17.743" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" '); insert into logs (id, log_tags) values (36,null); insert into logs (id, log_tags) values (37,'</Event>'); db<>小提琴 注意:在我的后续查询(步骤#1 和#2)中,我选择省略 log_tags is null 的行。因此,在实践中,每组最少 2 行,每组最多 5 行。 我的最终目标是数据透视。每组 log_tags 将旋转成 5 列。 像这样: 作为新手,我觉得有几个步骤: 已解决:根据开始/结束标记将 GROUP_ID 分配给行 已解决:使用类型列对每个组的每个标签进行分类。 未解决:填补空白。每组应有 5 行,每种类型一行。 未解决:将每组标签旋转成 5 个类型的列。 未解决:将旋转类型列解析为其他列。 第 1 步和第 2 步(已解决): select --Step #1: sum(case when log_tags like '<Event%' then 1 else 0 end) over (order by id) group_id, --Step #2: case when substr(log_tags,1,13) = '<Event time="' then 1 when substr(log_tags,1, 9) = 'Database:' then 2 when substr(log_tags,1,10) = ' SQL:' then 3 when substr(log_tags,1,34) = ' Number of features returned:' then 4 when substr(log_tags,1, 8) = '</Event>' then 5 end as type, substr(log_tags,1,100) as log_tags from logs where log_tags is not null GROUP_ID TYPE LOG_TAGS ---------- ---------- ---------------------------------------------------------------------------------------------------- 1 1 <Event time="Sat Apr 15 1:13:17.750" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 1 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 1 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 1 5 </Event> 2 1 <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" 2 5 </Event> 3 1 <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" 3 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 3 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 3 4 Number of features returned: 100 3 5 </Event> 4 1 <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 4 5 </Event> 5 1 <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 5 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 5 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 5 5 </Event> 6 1 <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" 6 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 6 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 6 4 Number of features returned: 100 6 5 </Event> 7 1 <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" 7 5 </Event> 8 1 <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 8 5 </Event> 9 1 <Event time="Sat Apr 15 1:13:17.744" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 9 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 9 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 9 5 </Event> 10 1 <Event time="Sat Apr 15 1:13:17.743" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" 10 5 </Event> 问题: 我怎样才能解决步骤 #3 - 填补空白,这样每组有 5 行,每种类型一行? 像这样(*): GROUP_ID TYPE LOG_TAGS ---------- ---------- ---------------------------------------------------------------------------------------------------- 1 1 <Event time="Sat Apr 15 1:13:17.750" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 1 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 1 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 1 *4 1 5 </Event> 2 1 <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" 2 *2 2 *3 2 *4 2 5 </Event> 3 1 <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" 3 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 3 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 3 4 Number of features returned: 100 3 5 </Event> 4 1 <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 4 *2 4 *3 4 *4 4 5 </Event> 5 1 <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 5 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 5 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 5 *4 5 5 </Event> 6 1 <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" 6 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 6 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 6 4 Number of features returned: 100 6 5 </Event> 7 1 <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" 7 *2 7 *3 7 *4 7 5 </Event> 8 1 <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 8 *2 8 *3 8 *4 8 5 </Event> 9 1 <Event time="Sat Apr 15 1:13:17.744" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" 9 2 Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase 9 3 SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 9 *4 9 5 </Event> 10 1 <Event time="Sat Apr 15 1:13:17.743" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" 10 *2 10 *3 10 *4 10 5 </Event> 只要你知道有多少种类型,这就是一个简单的 pivpt。 WITH CTE AS (select --Step #1: (https://stackoverflow.com/a/76021834/5576771) sum(case when log_tags like '<Event%' then 1 else 0 end) over (order by id) group_id, --Step #2: case when substr(log_tags,1,13) = '<Event time="' then 1 when substr(log_tags,1, 9) = 'Database:' then 2 when substr(log_tags,1,10) = ' SQL:' then 3 when substr(log_tags,1,34) = ' Number of features returned:' then 4 when substr(log_tags,1, 8) = '</Event>' then 5 end as type, substr(log_tags,1,100) as log_tags from logs where log_tags is not null) SELECT group_id , MAX(CASE WHEN type = 1 then log_tags END ) type1 , MAX(CASE WHEN type = 2 then log_tags END ) type2 , MAX(CASE WHEN type = 3 then log_tags END ) type3 , MAX(CASE WHEN type = 4 then log_tags END ) type4 , MAX(CASE WHEN type = 5 then log_tags END ) type5 FROM CTE GROUP BY group_id ORDER BY group_id GROUP_ID 类型1 类型2 类型3 类型4 类型5 1 数据库:C:�3 Files\GDBs_4\New Mobile Geodatabase.geodatabase 〇〇〇〇〇SQL: 选择 main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 空 2 空 空 空 3 数据库:C:�3 Files\GDBs_4\New Mobile Geodatabase.geodatabase 〇〇〇〇〇SQL: 选择 main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 〇〇〇〇〇返回的特征数:100 4 空 空 空 5 数据库:C:�3 Files\GDBs_4\New Mobile Geodatabase.geodatabase 〇〇〇〇〇SQL: 选择 main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 空 6 数据库:C:�3 Files\GDBs_4\New Mobile Geodatabase.geodatabase 〇〇〇〇〇SQL: 选择 main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 〇〇〇〇〇返回的特征数:100 7 空 空 空 8 空 空 空 9 数据库:C:�3 Files\GDBs_4\New Mobile Geodatabase.geodatabase 〇〇〇〇〇SQL: 选择 main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO 空 10 空 空 空 小提琴 看起来完成所有步骤三和四所需的只是一个支点: select * from (select --Step #1: (https://stackoverflow.com/a/76021834/5576771) sum(case when log_tags like '<Event%' then 1 else 0 end) over (order by id) group_id, --Step #2: case when substr(log_tags,1,13) = '<Event time="' then 'a1' when substr(log_tags,1, 9) = 'Database:' then 'a2' when substr(log_tags,1,10) = ' SQL:' then 'a3' when substr(log_tags,1,34) = ' Number of features returned:' then 'a4' when substr(log_tags,1, 8) = '</Event>' then 'a5' end as type, substr(log_tags,1,100) as log_tags from logs where log_tags is not null) PIVOT( max(log_tags) FOR type IN ( 'a1','a2','a3','a4','a5' ) ) ORDER BY group_id 小提琴这里. 关于第 5 步:目前还不完全清楚你的目标到底是什么(很可能它很适合单独的问题),但你所需要的只是将标签 Event 的属性提取到单独的列中,然后查看进入XMLTABLE。这里有很多关于这个问题的问题。举个例子:这是 one 昨天问的。

回答 2 投票 0

根据开始/结束标签将 GROUP_ID 分配给行

我有一个名为 LOGS 的 Oracle 18c 表: 创建表日志(id 号,log_tags varchar2(4000)); 插入日志 (id, log_tags) 值 ( 1,' 我有一个名为 LOGS 的 Oracle 18c 表: create table logs (id number, log_tags varchar2(4000)); insert into logs (id, log_tags) values ( 1,'<Event time="Sat Apr 15 1:13:17.750" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values ( 2,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values ( 3,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values ( 4,'</Event>'); insert into logs (id, log_tags) values ( 5,'<Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" '); insert into logs (id, log_tags) values ( 6,null); insert into logs (id, log_tags) values ( 7,'</Event>'); insert into logs (id, log_tags) values ( 8,'<Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" '); insert into logs (id, log_tags) values ( 9,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (10,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (11,' Number of features returned: 100'); insert into logs (id, log_tags) values (12,'</Event>'); insert into logs (id, log_tags) values (13,'<Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (14,null); insert into logs (id, log_tags) values (15,'</Event>'); insert into logs (id, log_tags) values (16,'<Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (17,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (18,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (19,'</Event>'); insert into logs (id, log_tags) values (20,'<Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" '); insert into logs (id, log_tags) values (21,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (22,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (23,' Number of features returned: 100'); insert into logs (id, log_tags) values (24,'</Event>'); insert into logs (id, log_tags) values (25,'<Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" '); insert into logs (id, log_tags) values (26,null); insert into logs (id, log_tags) values (27,'</Event>'); insert into logs (id, log_tags) values (28,'<Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (29,null); insert into logs (id, log_tags) values (30,'</Event>'); insert into logs (id, log_tags) values (31,'<Event time="Sat Apr 15 1:13:17.744" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" '); insert into logs (id, log_tags) values (32,'Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase'); insert into logs (id, log_tags) values (33,' SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO'); insert into logs (id, log_tags) values (34,'</Event>'); insert into logs (id, log_tags) values (35,'<Event time="Sat Apr 15 1:13:17.743" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" '); insert into logs (id, log_tags) values (36,null); insert into logs (id, log_tags) values (37,'</Event>'); db<>小提琴 日志组总是以 <Event ... 行开始,以 </Event> 行结束。 每组最少3行,每组最多5行。 行通过 ID 列按顺序排列。 对于每个组,我想分配一个GROUP_ID号: 问题: 在 SQL 查询中,如何根据开始/结束标记将 GROUP_ID 分配给行? 你有 gaps and islands 问题,一种选择是使用窗口函数 sum() 结合 case when 子句来定义所需的组: select 1 + sum(case when log_tags = '</Event>' then 1 else 0 end) over (order by id) - case when log_tags = '</Event>' then 1 else 0 end as GROUP_ID, log_tags from logs 这里演示 查询 1: with cte as( select id, (case when substr(log_tags,1,6) = '<Event' then (row_number()over(partition by substr(log_tags,1,6) order by id)) end) rn, log_tags from logs ) select last_value(rn ignore nulls) over(order by id rows between unbounded preceding and 0 preceding) group_id,id,log_tags from cte 结果: | GROUP_ID | ID | LOG_TAGS | |----------|----|------------------------------------------------------------------------------------------------------| | 1 | 1 | <Event time="Sat Apr 15 1:13:17.750" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" | | 1 | 2 | Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase | | 1 | 3 | SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO | | 1 | 4 | </Event> | | 2 | 5 | <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" | | 2 | 6 | (null) | | 2 | 7 | </Event> | | 3 | 8 | <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" | | 3 | 9 | Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase | | 3 | 10 | SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO | | 3 | 11 | Number of features returned: 100 | | 3 | 12 | </Event> | | 4 | 13 | <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" | | 4 | 14 | (null) | | 4 | 15 | </Event> | | 5 | 16 | <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" | | 5 | 17 | Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase | | 5 | 18 | SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO | | 5 | 19 | </Event> | | 6 | 20 | <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2" | | 6 | 21 | Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase | | 6 | 22 | SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO | | 6 | 23 | Number of features returned: 100 | | 6 | 24 | </Event> | | 7 | 25 | <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" | | 7 | 26 | (null) | | 7 | 27 | </Event> | | 8 | 28 | <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" | | 8 | 29 | (null) | | 8 | 30 | </Event> | | 9 | 31 | <Event time="Sat Apr 15 1:13:17.744" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0" | | 9 | 32 | Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase | | 9 | 33 | SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO | | 9 | 34 | </Event> | | 10 | 35 | <Event time="Sat Apr 15 1:13:17.743" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1" | | 10 | 36 | (null) | | 10 | 37 | </Event> | 几个解析函数:row_number() 来创建组id,导致得到下一个“ select start_evt.grp, other_evt.id, other_evt.log_tags from ( select row_number() over(order by l1.id) as grp, l1.id, l1.log_tags, nvl(lead(l1.id) over(order by l1.id),999999999) as next_id from logs l1 where l1.log_tags like '<Event time="%' ) start_evt join logs other_evt on other_evt.id between start_evt.id and start_evt.next_id-1 ;

回答 3 投票 0

SQL 中的时间聚类

我有一个时间戳列表,我想将它们聚集在一起。例如,给定以下时间戳: 2022-01-02T03:04:00 2022-01-02T03:05:00 2023-04-05T06:07:00 2023-04-05T06:08:00 2023-04-0...

回答 1 投票 0

T-SQL 查询以查找高尔夫数据库中低于标准杆分数的最连续数

假设我们的高尔夫球场有一个 MsSQL 数据库。我们有一个名为 Results 的表,其中包含以下列(均为 INT 且 NOT NULL): 玩家ID 圆号 孔号 分数 我们还有一张桌子...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.