Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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 - Fatal编程技术网

Sql 订购后是否添加新的滞后列?

Sql 订购后是否添加新的滞后列?,sql,sql-server,Sql,Sql Server,我试图在订购后生成一个滞后列,但不确定如何做到这一点 SELECT Date ,Vehicle ,OdoReadingMiles FROM [Fleet].[DwhTriscanChargeouts] order by convert(datetime, date, 103), Vehicle ASC LAG (OdoReadingMiles,1) Over (ORDER BY Vehicle) AS New 在使用Lag函数之前,我希望能

我试图在订购后生成一个滞后列,但不确定如何做到这一点

SELECT 
     Date
      ,Vehicle
      ,OdoReadingMiles   
  FROM [Fleet].[DwhTriscanChargeouts]
  order by convert(datetime, date, 103), Vehicle ASC
  LAG (OdoReadingMiles,1) Over (ORDER BY Vehicle) AS New

在使用Lag函数之前,我希望能够按日期顺序对数据集进行排序。我以前试过使用Lag函数,但似乎不起作用?

我想您需要
分区:

SELECT Date, Vehicle, OdoReadingMiles,
       LAG(OdoReadingMiles, 1) OVER (PARTITION BY Vehicle ORDER BY date) AS prev_OdoReadingMiles
FROM [Fleet].[DwhTriscanChargeouts];

请提供样本数据和期望的结果,以解释您正在尝试做什么。这可能是因为订单之间缺少
。谢谢。我觉得我的第一篇文章不够清楚。我想要的是数据集按日期和车辆编号排序,然后使用滞后函数。我已经尝试了上述代码的变体,但没有luck@AmmarWahid . . . 如果您想要特定的订单,请尝试在外部查询中添加一个
orderby
。实际上,查看不同车辆上的里程表读数是没有意义的,所以这个查询做了一些有用的事情。