1

我需要进行一个将显示以下内容的 sql 查询:

'截至当前日期超过 90 天的活跃帐户总数'

日期来自一个 sql 查询,它是这样的:

select date from currentdatetable

现在我的查询是计算来自 accounttable 的所有活动帐户,所以我的查询如下所示:

select count(*) from accountstable

我的问题是如何声明 accounttable 中的日期比 currentdatetable 的日期早 90 天?

我当前的代码:

select count(*) from accountstable at where at.date > (select date from currentdatetable)
4

3 回答 3

0
select count(*) from accountstable at where at.date > (select dateadd(day, -90,date) from currentdatetable)
于 2013-04-03T10:57:01.340 回答
0

使用 MS SQL-Server,您可以使用DATEADD+ DATEDIFF

SELECT Count = Count(*),
       Date = (SELECT TOP 1 [date] 
               FROM   currentdatetable)
FROM   accountstable a 
WHERE  Datediff(dd, a.[date], (SELECT TOP 1 [date] 
                               FROM   currentdatetable)) > 90 

演示

于 2013-04-03T11:07:19.810 回答
-1

您可以只选择更大的日期来优化上层答案:

select count(*) from accounttable at where at.date > (select TOP 1 dateadd(day, -90,date) from currentdatetable ORDER BY date DESC)

于 2013-04-03T10:57:10.440 回答