sql - 在列中查找值之前和之后的值
问题描述
我正在使用该lag()
函数来查找先前在同一字段中出现的任何特定值的值。
是否有功能可以在字段值的下一个/之后找到一个?
示例数据:
id desc
1 line1
1 line2
1 line 3
2 line 1
2 line 2
2 line 3
预期结果:
current lg_desc next
line 2 line1 line 3
line 2 line1 line 3
到目前为止我的查询是:
select Desc as current,
lag(Description)
over (partition by ID order by TimestampUTC) as lg_desc
from #temp1
解决方案
像这样使用引导功能
select Desc as current,
lag(Description)
over (partition by ID order by TimestampUTC) as lg_desc,
lead(Description)
over (partition by ID order by TimestampUTC) as ld_desc
from #temp1
推荐阅读
- reactjs - 使用 React 返回父 div onClick
- selenium - 有没有办法使用 Appium 使用部分文本或完整文本单击元素
- select - APDU 选择应用程序返回 SW 6982
- python - 你如何在 Python 中随时间建模?
- python - pyparsing——用函数扩展fourFn.py示例
- google-apps-script - 带别名的 GmailApp.sendEmail 语法
- azure - Azure Web App 中用于容器的部署后脚本
- python - 如何使用 .get 在 Python 中通过列表框检索多个选择?
- php - 分配 $_SESSION 变量
- r - 为什么我的 sum() 函数不适用于 R 中的矩阵运算