SQL Server Datediff()函数不起作用

时间:2019-03-11 17:28:43

标签: sql sql-server db2 datediff

对于SQL Server,我很难理解时间之间的差异

我正在使用SQuirell客户端,但不确定Datediff是否为函数

我的数据示例

Person | Times Ordered | Date order placed | Date order placed (Timestamp) 
---------------------------------------------------------------------
Jane   |      1        |   5/5/2017        |  5/5/2017 01:01:56.95
Jane   |      2        |   6/2/2017        |  6/2/2017 18:41:34.486
Jane   |      3        |   7/5/2018        |  7/5/2018 08:31:45.759
Donald |      1        |   5/2/2017        |  5/2/2017 21:22:44.23 
Donald |      2        |   8/2/2018        |  8/2/2018 22:25:50.323 
Ron    |      1        |   4/8/2017        |  4/8/2017 03:10:25.55
Ron    |      2        |   8/30/2017       |  8/30/2017 19:20:20.55
Ron    |      3        |   5/24/2018       |  5/24/2018 09:35:55.85
Jim    |      1        |   2/15/2017       |  2/15/2017 20:15:55.45 
Jim    |      2        |   9/6/2018        |  9/6/2018 23:20:50.4

我尝试了以下SQL Server查询

select
a.Person,
a.times_ordered,
a.date_order_placed,
datediff(month, lag(a.Date_order_placed_ts) over(partition by a.persion order by a.date_order_placed)) as Month_Difference
from table_1 a

我遇到的错误

SQLERRMC:MONTH SQL状态:42703 错误代码:-206

SQLERRMC:MONTH SQL状态:56098 错误代码:-727

我正在尝试弄清最近的订单与之前的订单之间的时间差。

所需的输出示例

Person | Times Ordered | Date order placed | Date order placed (Timestamp) | Month_difference
----------------------------------------------------------------------------------------------
Jane   |      1        |   5/5/2017        |  5/5/2017 01:01:56.95         | <Null>
Jane   |      2        |   6/2/2017        |  6/2/2017 18:41:34.486        |   1
Jane   |      3        |   7/5/2018        |  7/5/2018 08:31:45.759        |   13
Donald |      1        |   5/2/2017        |  5/2/2017 21:22:44.23         | <Null>
Donald |      2        |   8/2/2018        |  8/2/2018 22:25:50.323        |   15
Ron    |      1        |   4/8/2017        |  4/8/2017 03:10:25.55         | <Null>
Ron    |      2        |   8/30/2017       |  8/30/2017 19:20:20.55        |   4
Ron    |      3        |   5/24/2018       |  5/24/2018 09:35:55.85        |   9
Jim    |      1        |   2/15/2017       |  2/15/2017 20:15:55.45        | <Null>
Jim    |      2        |   9/6/2018        |  9/6/2018 23:20:50.4          |   19

1 个答案:

答案 0 :(得分:0)

db2中没有这样的功能。
尝试使用TIMESTAMPDIFF