我有一张个人眼科检查和眼镜购买日期的表格。我只想保留个体在进行眼科检查后购买眼镜的情况。在下面的示例中,我想保留人员2,人员3而不是人员4的人员1,事件2和3。如何在SQL Server中做到这一点?
| Person | Event | Order |
| 1 | Exam | 1 |
| 1 | Eyewear| 2 |
| 2 | Eyewear| 1 |
| 2 | Exam | 2 |
| 2 | Eyewear| 3 |
| 3 | Exam | 1 |
| 3 | Eyewear| 2 |
| 4 | Eyewear| 1 |
| 4 | Exam | 2 |
最终结果看起来像
| Person | Event | Order |
| 1 | Exam | 1 |
| 1 | Eyewear| 2 |
| 2 | Exam | 2 |
| 2 | Eyewear| 3 |
| 3 | Exam | 1 |
| 3 | Eyewear| 2 |
最佳答案
自我加入应该起作用...
select
t.Person
,t.Event
,t.[Order]
from
yourTable t
inner join
yourTable t2 on t2.Person = t.Person
and t2.[Order] = (t.[Order] +1)
where
t2.Event = 'Eyewear'
and t.Event = 'Exam'
关于sql - 仅包括事件顺序有效的分组观察,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47270331/