Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于截止日期和上一行获取行_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 基于截止日期和上一行获取行

Sql 基于截止日期和上一行获取行,sql,sql-server,tsql,Sql,Sql Server,Tsql,我得到了以下示例数据,我试图根据DateChanged值从链接表和前一行中获取适当的数据量 结果将显示为 这怎么可能呢?到目前为止,我只知道这一点,但我想知道还有更好的方法吗 select * from #temp t Inner Join ( select TempID, Amount, StartDate, isnull(EndDate, getdate()) as EndDate,

我得到了以下示例数据,我试图根据DateChanged值从链接表和前一行中获取适当的数据量

结果将显示为

这怎么可能呢?到目前为止,我只知道这一点,但我想知道还有更好的方法吗

 select
    *
from 
    #temp t
Inner Join 
(
    select 
        TempID,
        Amount,
        StartDate,
        isnull(EndDate, getdate()) as EndDate,
        LAG(Amount, 1)  OVER(partition by TempID ORDER BY row_num)  PrevValue2,
        LEAD(Amount, 1) OVER(partition by TempID ORDER BY row_num)  NextValue2
    from 
    (
      SELECT ROW_NUMBER() OVER (partition by TempID Order by TempID) row_num,
             TempAmountsID,
             TempID,
             StartDate,
             EndDate,
             Amount
      FROM   #tempAmounts
    ) t1
) t2 on 
    t.TempID = t2.TempID and
    t.DateChanged between t2.StartDate and t2.EndDate

这适用于提供的示例:

select a.*, b.amount , c.amount as previousamount
from #temp a
left join #tempamounts b on a.tempid = b.tempid 
       and b.startdate = (select max(startdate) from #tempamounts 
          where tempid = a.tempid and startdate < a.datechanged)
left join #tempamounts c on a.tempid = c.tempid
      and c.startdate = (select max(startdate) from #tempamounts 
          where tempid = a.tempid and startdate < b.startdate) 

这适用于提供的示例:

select a.*, b.amount , c.amount as previousamount
from #temp a
left join #tempamounts b on a.tempid = b.tempid 
       and b.startdate = (select max(startdate) from #tempamounts 
          where tempid = a.tempid and startdate < a.datechanged)
left join #tempamounts c on a.tempid = c.tempid
      and c.startdate = (select max(startdate) from #tempamounts 
          where tempid = a.tempid and startdate < b.startdate) 
只需使用lag和join:

只需使用lag和join:


1请不要使用图像作为数据。2.请出示您的护照attempt@DaleK,当然,谢谢,已添加尝试。我想显示我所指的数据,而不仅仅是sql命令。是的,您可以将数据显示为格式化文本,占用更少的空间,更易于阅读。1请不要将图像用于数据。2.请出示您的护照attempt@DaleK,当然,谢谢,已添加尝试。我想显示我所指的数据,而不仅仅是sql命令。是的,您可以将数据显示为格式化文本,占用更少的空间,更易于阅读。
select t.*, ta.amount, ta.prev_amount
from #temp t left join
     (select ta.*,
             lag(ta.amount) over (partition by ta.tempid order by ta.startdate) as prev_amount
      from #tempAmounts ta
     ) ta
     on t.tempid = ta.tempid and
        t.datechanged >= ta.startdate and
        (t.datechanged <= ta.enddate or ta.enddate is null);