MS SQL Server GETDATE - 90其中-90不像以前那样

时间:2017-08-02 14:14:39

标签: sql sql-server sql-server-2008

今天对我来说是2017-08-02 13:00:00.000我有这张桌子:

TABLE1        

    TIME                            NAME        PRODUCT
    2014-10-10 08:34:57.000         Bob          Glu
    2015-11-03 13:34:27.000         Bob          Finger
    2017-07-08 09:51:46.000         Bob          Note
    2017-07-08 09:51:46.000         Bob          PC
    2017-07-31 09:51:46.000         Bob          Car
    2017-07-31 09:51:46.000         Bob          Ball
    2017-07-31 09:51:46.000         Bob          Shoe
    2017-04-01 08:34:57.000         Alex         Pen
    2017-06-03 13:34:27.000         Alex         Horse
    2017-07-31 05:51:46.000         Alex         Bread
    2017-07-31 09:51:46.000         Alex         Hummus
    2017-07-31 09:51:46.000         Alex         Kitchen
    2017-07-31 09:51:46.000         Alex         Hell
    2017-07-31 09:51:46.000         Alex         Night
    2016-10-10 08:34:57.000         Eva          Mouse
    2017-05-03 13:34:27.000         Eva          Cement
    2017-06-31 06:51:46.000         Eva          Pc
    2017-06-31 03:51:46.000         Eva          Bread
    2017-06-31 08:51:46.000         Eva          Shoe
    2017-06-31 07:51:46.000         Eva          Ball
    2017-06-31 07:51:46.000         Eva          Zoo

我需要知道上表中丢失了哪些 PRODUCT ,但我只需知道上次丢失的 PRODUCT TIME + NAME )和 - 90天。我期待这样的结果:

TABLE1

    TIME                           NAME         Product

    2017-05-03 13:34:27.000         Eva          Cement
    2017-06-03 13:34:27.000         Alex         Horse
    2017-07-08 09:51:46.000         Bob          Note
    2017-07-08 09:51:46.000         Bob          PC

该表拥有数百万个名称和数百万种产品。目前,我对上次更新后的90天感兴趣。

我正在尝试

SELECT * from table1
WHERE  (TIME > getdate ()- 90) AND (TIME not like getdate ()- 90)

3 个答案:

答案 0 :(得分:1)

尝试where DATEDIFF(day, TIME, GETDATE()) < 90

答案 1 :(得分:0)

如果您想要从今天开始的90天内的行。

select * from table1
where TIME >= getdate()-90

答案 2 :(得分:0)

你想弄清楚每个人在过去90天里丢失的第一件事吗?并包括所有关系?

允许限制数据并在cte中添加窗口函数,然后将数据限制为#1

;with cte as 
(
  SELECT * , DENSE_RANK() over (partition by [name] order by [time]) as DR  
  from table1
  WHERE [time] >= dateadd('d',-90,getdate()) 
)

select *
from cte
where dr=1