Sql 搜索事件和紧挨着前面的事件
以下是表格的布局(仅显示相关行): 我想提取eventid 300010的所有事件,这些事件后面紧跟着eventid 300020-关键是我的表有800个不同的id,我需要它查找相同id的前一个事件 所以我的结果是这样的:Sql 搜索事件和紧挨着前面的事件,sql,sql-server,Sql,Sql Server,以下是表格的布局(仅显示相关行): 我想提取eventid 300010的所有事件,这些事件后面紧跟着eventid 300020-关键是我的表有800个不同的id,我需要它查找相同id的前一个事件 所以我的结果是这样的: B 2020-01-21 13:02:45.000 300010 B 2020-01-21 13:02:90.000 300020 非常感谢您的帮助。您可以这样做: 对于相同的id,给我事件300010,事件300020在后面
B 2020-01-21 13:02:45.000 300010
B 2020-01-21 13:02:90.000 300020
非常感谢您的帮助。您可以这样做: 对于相同的id,给我事件300010,事件300020在后面,但两者之间没有事件
WITH CTE AS
(
SELECT 'A' AS id, '2020-01-21 13:02:00.000' AS timestamp, 300010 AS eventno
UNION SELECT'B', '2020-01-21 13:02:45.000', 300010
UNION SELECT'E', '2020-01-21 13:02:50.000', 300010
UNION SELECT'B', '2020-01-21 13:02:90.000', 300020
UNION SELECT 'M', '2020-01-21 13:03:56.000', 300010
)
select *
from CTE a
inner join CTE b
on a.eventno = 300010 and b.eventno = 300020 and a.timestamp < b.timestamp and a.id = b.id
and not exists (
select 1
from CTE c
where c.timestamp > a.timestamp
and c.timestamp < b.timestamp and c.id = b.id
)
如果希望一行位于另一行的下方:
WITH CTE AS
(
SELECT 'A' AS id, '2020-01-21 13:02:00.000' AS timestamp, 300010 AS eventno
UNION SELECT'B', '2020-01-21 13:02:45.000', 300010
UNION SELECT'E', '2020-01-21 13:02:50.000', 300010
UNION SELECT'B', '2020-01-21 13:02:90.000', 300020
UNION SELECT 'M', '2020-01-21 13:03:56.000', 300010
)
select a.*
from CTE a
inner join CTE b
on a.eventno = 300010 and b.eventno = 300020 and a.timestamp < b.timestamp and a.id = b.id
and not exists (
select 1
from CTE c
where c.timestamp > a.timestamp
and c.timestamp < b.timestamp and c.id = b.id
)
union
select b.*
from CTE a
inner join CTE b
on a.eventno = 300010 and b.eventno = 300020 and a.timestamp < b.timestamp and a.id = b.id
and not exists (
select 1
from CTE c
where c.timestamp > a.timestamp
and c.timestamp < b.timestamp and c.id = b.id
)
order by id, eventno
ms sql server mgmt studioFYI SSMS不是DBMS,它只是一个客户端接口。当您说“我需要它查找相同id的前一个事件”时,我假设仅针对事件号300010和300020。这就是你的意思吗?它不会返回表中与问题中相同的任何行。更新:对不起,是的。我的错误。我不小心将
eventid
改为id
,而不是eventno
@Scratte,为我的答案添加了更多细节。Thx@Scrattle,将别名更改为非内部别名,否则将生成错误。更改为x,则查询为perfect@zip哦这对SQL Server来说是特别的吗?我猜,SQL说:Msg 156,级别15,状态1,第22行关键字“内部”附近的语法不正确。但是一旦换成其他的,它就可以工作了。谢谢你对它的测试:)我只有一个SQLite。像冠军一样工作……你帮了我,教了我一些东西——非常感谢。
id timestamp eventno id timestamp eventno
B 2020-01-21 13:02:45.000 300010 B 2020-01-21 13:02:90.000 300020
WITH CTE AS
(
SELECT 'A' AS id, '2020-01-21 13:02:00.000' AS timestamp, 300010 AS eventno
UNION SELECT'B', '2020-01-21 13:02:45.000', 300010
UNION SELECT'E', '2020-01-21 13:02:50.000', 300010
UNION SELECT'B', '2020-01-21 13:02:90.000', 300020
UNION SELECT 'M', '2020-01-21 13:03:56.000', 300010
)
select a.*
from CTE a
inner join CTE b
on a.eventno = 300010 and b.eventno = 300020 and a.timestamp < b.timestamp and a.id = b.id
and not exists (
select 1
from CTE c
where c.timestamp > a.timestamp
and c.timestamp < b.timestamp and c.id = b.id
)
union
select b.*
from CTE a
inner join CTE b
on a.eventno = 300010 and b.eventno = 300020 and a.timestamp < b.timestamp and a.id = b.id
and not exists (
select 1
from CTE c
where c.timestamp > a.timestamp
and c.timestamp < b.timestamp and c.id = b.id
)
order by id, eventno
id timestamp eventno
B 2020-01-21 13:02:45.000 300010
B 2020-01-21 13:02:90.000 300020
SELECT id, timestamp, eventno
FROM (SELECT id,
timestamp,
eventno,
LAG(eventno) OVER (
PARTITION BY id
ORDER BY timestamp) previous_eventno,
LEAD(eventno) OVER (
PARTITION BY id
ORDER BY timestamp) next_eventno
FROM table
) AS x
WHERE (x.eventno = 300020 and x.previous_eventno = 300010)
OR (x.eventno = 300010 and x.next_eventno = 300020);