Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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,我的公司使用远程信息处理设备捕获资产驱动的英里数范围内的数据。我希望得到的是任何给定月份的最后一个里程表读数——上个月的最后一个里程表读数。我认为窗口函数可能是获得每天最大里程表值的最佳方法(参见下面的示例) 我遇到的一个问题是试图概念化如何按日期获取给定月份资产的最后一个条目(例如,如果有2018年6月28日、2018年6月29日和2018年6月30日的条目,我只想要2018年6月30日的数据,并过滤掉除此之外的任何其他信息)。此外,然后减去任何给定月份的最大里程表-前一个月的最大里程表(见下

我的公司使用远程信息处理设备捕获资产驱动的英里数范围内的数据。我希望得到的是任何给定月份的最后一个里程表读数——上个月的最后一个里程表读数。我认为窗口函数可能是获得每天最大里程表值的最佳方法(参见下面的示例)

我遇到的一个问题是试图概念化如何按日期获取给定月份资产的最后一个条目(例如,如果有2018年6月28日、2018年6月29日和2018年6月30日的条目,我只想要2018年6月30日的数据,并过滤掉除此之外的任何其他信息)。此外,然后减去任何给定月份的最大里程表-前一个月的最大里程表(见下面的示例)

示例查询数据:

|Date      |Asset #|Location   |Max Odo|
|6/30/2018 |1215   |Phoenix, AZ|17500  |
|6/29/2018 |1215   |Phoenix, AZ|17250  |
|6/28/2018 |1215   |Phoenix, AZ|17000  |
|…         |…      |…          |…      | 
|5/31/2018 |1215   |Phoenix, AZ|15000  |
|…         |…      |…          |…      |
|4/30/2018 |1215   |Phoenix, AZ|12000  |
所需的查询结果:

|Date      |Asset #|Location   |Odo Var|
|6/30/2018 |1215   |Phoenix, AZ|2500   |
|5/31/2018 |1215   |Phoenix, AZ|3000   |
有没有一个简单的方法可以做到这一点?如有任何见解,将不胜感激


谢谢大家!

这与Brian的方法类似,但它修复了一些错误:

;With CTE_odo
As
(
Select 
    ore.Reading_Date
    , ore.Asset
    , ore.Location
    , ore.Reading
    , Row_Number() Over (Partition By ore.Asset, EOMonth(ore.Reading_Date) Order By ore.Reading_Date Desc) As RN
From dbo.Odo_Reading As ore
)

Select 
odo.Reading_Date
, odo.Asset
, odo.Location
, odo.Reading
, Lag(odo.Reading, 1) Over (Order By odo.Reading_Date) As Previous_Reading
, odo.Reading - Lag(odo.Reading, 1) Over (Order By odo.Reading_Date) As Month_Milage
From CTE_odo As odo
Where odo.RN = 1
-- this restricts the return to the last two months
And EOMonth(odo.Reading_Date) >= DateAdd(Month, -2, GetDate())
With r As (
      select r.* 
             row_Number() over (partition By r.Asset, EOMonth(r.Reading_Date) order by r.Reading_Date Desc) as seqnum
      from dbo.Odo_Reading As ore
     )
select reading_date, asset, location, reading,
       (reading - coalesce(prev_reading, 0)) as diff
from (select r.*,
             lag(r.reading, 1) Over (partition by asset order By r.Reading_Date) as Previous_Reading
      from r
      where r.seqnum = 1
     ) r
-- this restricts the return to the last two months
where EOMonth(r.Reading_Date) >= DateAdd(Month, -2, GetDate())

编辑问题并添加一些示例数据&期望的结果会有所帮助。@Yogeshharma感谢您的回答!我添加了一些示例数据和所需的结果。让我知道这一切是否有意义。您使用的是什么版本的SQL Server?@Brian SQL Server2012@TabAlleman. . . 此问题与您提供的问题不同。请你打开问题。当我测试我发布的代码时,我遗漏了什么错误?@Brian。您错过了按划分的
和滞后的默认值。另一方面,就像使用
eomonth()
。我认为CTE中的
分区
涵盖了它,我喜欢在
前导
滞后
上有一个
NULL
作为哨兵值。是的,我是《EOMonth》的超级粉丝——我经常使用它。
With r As (
      select r.* 
             row_Number() over (partition By r.Asset, EOMonth(r.Reading_Date) order by r.Reading_Date Desc) as seqnum
      from dbo.Odo_Reading As ore
     )
select reading_date, asset, location, reading,
       (reading - coalesce(prev_reading, 0)) as diff
from (select r.*,
             lag(r.reading, 1) Over (partition by asset order By r.Reading_Date) as Previous_Reading
      from r
      where r.seqnum = 1
     ) r
-- this restricts the return to the last two months
where EOMonth(r.Reading_Date) >= DateAdd(Month, -2, GetDate())