如何使用 LAG 函数删除我的查询结果?

Posted

技术标签:

【中文标题】如何使用 LAG 函数删除我的查询结果?【英文标题】:How i can remove results of my Query, using LAG function? 【发布时间】:2020-06-30 12:13:13 【问题描述】:

首先,对不起我的英语不好。

我需要计算分隔符组的中间时间。实际上我有以下查询:

USE mydatabase
SELECT p.iddoc as SaleOrder, p.name as Client, u.username as SysUser,

    CONVERT(DATETIME, o.creatdate) as CreateDate,
    CONVERT(DATETIME, o.inidate) as IniDate,
    CONVERT(DATETIME, o.findate) as FinalDate,

      RIGHT('0' + CAST(DATEDIFF(S, (lag(o.findate, 1) OVER (ORDER BY o.findate)), o.inidate) / 3600 AS VARCHAR(2)),2) + ':'
    + RIGHT('0' + CAST(DATEDIFF(S, (lag(o.findate, 1) OVER (ORDER BY o.findate)), o.inidate) % 3600/60 AS VARCHAR(2)),2) + ':'
    + RIGHT('0' + CAST(DATEDIFF(S, (lag(o.findate, 1) OVER (ORDER BY o.findate)), o.inidate) % 60 AS VARCHAR(2)),2) as IdleTime

FROM serviceorder o
    inner join user u on o.usercode = u.codusuario
    inner join requests p on o.idrequest = p.codpedidocab

WHERE u.username like ('USER NAME')
    and o.iduserstart is NOT NULL
    and p.sit = '8'
    and o.creatdate BETWEEN '2020-06-30 00:00:00' and '2020-06-30 23:59:00'
    and o.inidate BETWEEN '2020-06-30 00:00:00' and '2020-06-30 23:59:00'

ORDER BY o.inidate

我在这个查询上的问题是当“o.inidate

你建议我做什么?

【问题讨论】:

我建议您添加一些示例数据和预期结果。请阅读***.com/help/how-to-ask 使用 CTE,然后在外部选择中过滤。 inner join user 也不行; USER 是保留关键字,应避免将其用于对象名称。 【参考方案1】:

窗口函数只能在selectorder by 子句中使用。您可以改用子查询:

SELECT *
FROM (
    SELECT 
        p.iddoc as SaleOrder, 
        p.name as Client, 
        u.username as SysUser,
        CONVERT(DATETIME, o.creatdate) as CreateDate,
        CONVERT(DATETIME, o.inidate) as IniDate,
        CONVERT(DATETIME, o.findate) as FinalDate,
        LAG(o.findate) OVER (ORDER BY o.findate) lagFinalDate,
        RIGHT('0' + CAST(DATEDIFF(S, (lag(o.findate, 1) OVER (ORDER BY o.findate)), o.inidate) / 3600 AS VARCHAR(2)),2) + ':'
            + RIGHT('0' + CAST(DATEDIFF(S, (lag(o.findate, 1) OVER (ORDER BY o.findate)), o.inidate) % 3600/60 AS VARCHAR(2)),2) + ':'
            + RIGHT('0' + CAST(DATEDIFF(S, (lag(o.findate, 1) OVER (ORDER BY o.findate)), o.inidate) % 60 AS VARCHAR(2)),2) as IdleTime
    FROM serviceorder o
    INNER JOIN [user] u     ON o.usercode  = u.codusuario
    INNER JOIN requests p ON o.idrequest = p.codpedidocab
    WHERE 
        u.username = 'USER NAME'
        AND o.iduserstart is NOT NULL
        AND p.sit = '8'
        AND o.creatdate >= '2020-06-30' 
        AND o.creatdate <  '2020-07-01'
        AND o.inidate   >= '2020-06-30' 
        AND o.inidate   <  '2020-07-01'
) t
where IniDate < lagFinalDate

对查询的其他更改:

半开区间过滤器比between更准确(也更容易理解)

u.username like ('USER NAME') 等价于u.username = 'USER NAME'

user是语言关键字,所以作为表名使用时需要加引号

【讨论】:

"LAG(o.findate) OVER (ORDER BY o.findate) lagFinalDate" 谢谢。

以上是关于如何使用 LAG 函数删除我的查询结果?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 oracle pl sql 中使用 LAG 函数,直到到达非零或最后一行?

如何比较mysql中的LAG函数

如何在spark sql lag函数中添加if或case条件

MYSQL lag() 和lead()函数使用介绍

如何删除使用 array_agg postgres 函数生成的重复项

如何在 MySQL 8(如 MSSQL)上使用表达式作为 LAG() 第二个参数?