首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL - LEAD()函数

SQL - LEAD()函数
EN

Stack Overflow用户
提问于 2020-08-25 04:58:54
回答 2查看 65关注 0票数 0

我尝试使用以下代码:

代码语言:javascript
复制
 SELECT SkillTargetID, DateTime,
    lead(EventName,1) over (partition by EventName order by DateTime) as next_eventname,
    lead(ReasonCode,1) over (partition by ReasonCode order by DateTime) as next_ReasonCode
 FROM ucce1_awdb.dbo.Agent_State_Trace ast
 WHERE 
 EventName = 3 and
 ReasonCode= 0 and
 next_eventname = 0 and
 next_ReasonCode = 114

但是由于next_eventnamenext_ReasonCode的原因,我无法执行该查询。有人能建议我该如何处理这件事吗?

EN

回答 2

Stack Overflow用户

发布于 2020-08-25 05:01:57

您需要一个子查询:

代码语言:javascript
复制
SELECT SkillTargetID, DateTime, next_eventname, next_ReasonCode
FROM (SELECT ast.*
             lead(EventName) over (partition by EventName order by DateTime) as next_eventname,
             lead(ReasonCode) over (partition by ReasonCode order by DateTime) as next_ReasonCode
      FROM ucce1_awdb.dbo.Agent_State_Trace ast
     ) ast
WHERE EventName = 3 and
      ReasonCode= 0 and
      next_eventname = 0 and
      next_ReasonCode = 114
票数 1
EN

Stack Overflow用户

发布于 2020-08-25 05:02:27

您不能在where子句中使用在select子句中定义的别名...并且不能在where子句中使用窗口函数。您需要将筛选移动到外部查询:

代码语言:javascript
复制
SELECT *
FROM (
    SELECT 
        SkillTargetID, 
        DateTime,
        lead(EventName,1) 
            over(partition by EventName order by DateTime) as next_eventname,
        lead(ReasonCode,1) 
            over(partition by ReasonCode order by DateTime) as next_ReasonCode
    FROM ucce1_awdb.dbo.Agent_State_Trace ast
) t
WHERE 
    EventName = 3 
    AND ReasonCode= 0
    AND next_eventname = 0 
    AND next_ReasonCode = 114

请注意,我将所有过滤器移到了外部查询(不仅仅是那些应用于窗口计算的过滤器):这是因为您(可能!)在应用过滤器之前,需要窗口函数来查看整个数据集。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63568649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档