使用滚动窗口从 Sybase SQL 数据库中提取数据

使用滚动窗口从 Sybase SQL 数据库中提取数据

问题描述:

我希望从数据库中提取三年的数据,但现在我必须使用 between 语句在代码的多个部分中指定日期:

I wish to pull three years of data from a database but right now I have to specify the dates in a number of sections in my code using a between statement:

BETWEEN '2015-10-01' AND '2018-09-30'

由于数据库只包含上个月的有效数据,所以我想把上个月的月底倒回去三年.

Since the database only contains valid data from the previous month backwards, I wish to take the end of the last month and go back three years.

我找到了 this 教程,作者展示了如何在 SQL Server 中执行此操作,我已尝试对其进行调整,但我的 RDBMS 在 datediff 函数中引发错误

I found this tutorial where the author shows how to do this in SQL server and I've tried to adapt it but my RDBMS is throwing errors in the datediff function

----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
LastDay_PreviousMonth

我的代码如下所示,但我看到的错误是关于将 0 转换为日期.

My code looks as follows but I the error I am seeing is about converting 0 to a date.

DECLARE @date DATE
SET @date = getdate()

SELECT dateadd(second,-1,dateadd(mm, DATEDIFF(m,0,GETDATE()),0))

如果有人有任何建议,我将非常感谢您的指导.

If anyone has any suggestions I would be very grateful for your guidance.

在你的 WHERE 子句中,你可以使用这个条件:

In your WHERE clause, you could use this condition:

DATEDIFF(month, [YourDateColumn], GETDATE()) BETWEEN 1 AND 36