返回连续行之间的列值



我有一个返回许多列的表,其中 2 列是代表工人班次的"入"和"出"的时间戳。我想为介于 2 个给定时间戳之间的记录返回第三列的值。问题是我的 2 个时间戳记录之间有行,我还需要第 3 列值。

例如:

ID 日期 输入 颜色 位置====     ====     ==     ===     ====     ========1 09/20 09:00 17:00 黑色巴黎2 09/21 09:00 空黑巴黎3 09/21 空 空 白 伦敦4 09/21 空 空 红 伦敦5 09/21 空 20:00 蓝色伦敦6 09/22 09:00 空黑巴黎7 09/22 空 空 白 伦敦8 09/22 空 空红 巴黎9 09/22 空 17:00 蓝色伦敦

在此示例中,我希望Color列的所有值都用于跨19:00班次的所有记录。因此,我想仅将 ID 的 Color2,3,4, and 5作为仅作为交叉19:00 09/21移位返回。

似乎您需要填写inout时间,它们在哪里NULL. 您可以使用相关的子查询来执行此操作。

select t1.*
from (select t1.*,
             (select top 1 in
              from t t2
              where t2.in is not null and
                    t2.id <= t.id
              order by t2.id desc
             ) as RealIn,
             (select top 1 out
              from t t2
              where t2.out is not null and
                    t2.id >= t.id
              order by t2.id asc
             ) as RealOut
      from t t1
     ) t1
where '19:00' between RealIn and RealOut;

最新更新