根据日期(过去30天)获取记录[重复]

时间:2022-08-25 17:52:54

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to get the records based on the last 30 days of the comment_date and a create_user_id. So for user_id 'BOB' it shouldn't be returning any records since there are records for 2/13 and 2/14 (which is within the 30-days of the comment_date). The same goes for 'STEVE', 'JOHN' and 'KEN'. How do I write the query for user_id 'TOM'(case 2), 'KEN'(case 3), and 'ROSS'(case 4)? Thanks

我正在尝试根据comment_date和create_user_id的最后30天获取记录。因此对于user_id'BOB',它不应该返回任何记录,因为有2/13和2/14的记录(在comment_date的30天内)。 “STEVE”,“JOHN”和“KEN”也是如此。如何为user_id'TOM'(案例2),'KEN'(案例3)和'ROSS'(案例4)编写查询?谢谢

comments_sk case    comments         create_user_id   comment_date
120          1      user_comment-1      JOHN           2/16/2017
121          1      user_comment-2      BOB            2/14/2017
122          1      user_comment-3      BOB            1/13/2017
123          2      user_comment-1      BOB            2/13/2017
124          2      user_comment-2      STEVE          2/13/2017
125          2      user_comment-3      TOM            1/13/2017
126          3      user_comment-1      JOHN           2/12/2017
127          3      user_comment-2      JOHN           1/12/2017
128          3      user_comment-3      KEN            1/11/2017
129          4      user_comment-1      KEN            2/14/2017
130          4      user_comment-2      ROSS           1/7/2017

1 个解决方案

#1


1  

 SELECT * 
 FROM 
      Your_Table 
 WHERE 
      Comment_date >= DATEADD(day,-30, GETDATE())  
      and Create_user_id in ('KEN','TOM','ROSS')
      and Comments = ''

#1


1  

 SELECT * 
 FROM 
      Your_Table 
 WHERE 
      Comment_date >= DATEADD(day,-30, GETDATE())  
      and Create_user_id in ('KEN','TOM','ROSS')
      and Comments = ''