sql中同一个表的上下两行之间的某个字段相减有关问题

https://blog.csdn.net/cassiel33/article/details/9187767

https://bbs.csdn.net/topics/390908213

select a.id,a.数量 - b.数量
from abc a left join abc b on a.id + 1 = b.id

 

select
   datediff(mi,b.start_time ,a.end_time)
from
   (select id=row_number()over(order by getdate()),* from tb)a,
   (select id=row_number()over(order by getdate()),* from tb)b
where
   a.id=b.id-1

 

复制代码
SELECT a.RowId,
       b.RowId,
       a.CreateOn,
       b.CreateOn,
       DATEDIFF(SECOND, b.CreateOn, a.CreateOn)
FROM
(
    SELECT ROW_NUMBER() OVER (ORDER BY a.CreateOn ASC) AS RowId,
           a.CreateOn
    FROM dbo.tbm_tpl_TaskProcessorLog a
        INNER JOIN dbo.tbm_sjb_ScheduledJob AS b
            ON a.TaskID = b.ScheduledJobID
    WHERE a.TaskID = 1109
          AND a.CreateOn >= '20200317'
) AS a
    INNER JOIN
    (
        SELECT ROW_NUMBER() OVER (ORDER BY a.CreateOn ASC) AS RowId,
               a.CreateOn
        FROM dbo.tbm_tpl_TaskProcessorLog a
            INNER JOIN dbo.tbm_sjb_ScheduledJob AS b
                ON a.TaskID = b.ScheduledJobID
        WHERE a.TaskID = 1109
              AND a.CreateOn >= '20200317'
    ) AS b
        ON a.RowId - 1 = b.RowId;
复制代码

 

 

复制代码
;WITH t
AS (SELECT ROW_NUMBER() OVER (ORDER BY a.CreateOn ASC) AS RowId,
           a.CreateOn
    FROM dbo.tbm_tpl_TaskProcessorLog a
        INNER JOIN dbo.tbm_sjb_ScheduledJob AS b
            ON a.TaskID = b.ScheduledJobID
    WHERE a.TaskID = 1109
          AND a.CreateOn >= '20200317')
SELECT a.*,
       b.CreateOn AS BCreatedOn,
       val = DATEDIFF(SECOND, a.CreateOn, b.CreateOn)
FROM t a
    LEFT JOIN t AS b
        ON a.RowId + 1 = b.RowId;
复制代码

 

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(2548)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2016-03-17 Newtonsoft.Json
2016-03-17 ASP.NET MVC
2016-03-17 log4net
2016-03-17 AutoMapper introduction
2016-03-17 项目中如何使用NuGet添加类库
点击右上角即可分享
微信分享提示