简体   繁体   English

SQL日期检查在设定的天数内

[英]SQL date check within set number of days

Im trying to find out how many clients viewed a property within 14 days of May 20, 2004, either before or after. 我试图找出有多少客户在2004年5月20日之前或之后的14天内查看了一处房产。 Not really sure at all how to go about this. 根本不确定如何解决这个问题。

Im assuming i need to group it and use a having? 我假设我需要将它分组并使用一个?

EDIT: I am using oracle now 编辑:我现在正在使用oracle

select count(*)
from VIEWING
WHERE CLAUSE?

For a one time query with that specific date, 对于具有该特定日期的一次性查询,

select count(*) clients
from yourtable
where yourdatefield >= {d'2004-05-06'}
and yourdatefield < {d'2004-06-08'}

You might want to consult a calendar to see if those dates are correct. 您可能需要查阅日历以查看这些日期是否正确。

Edit #1, since you are using Oracle, you can use: 编辑#1,因为您使用的是Oracle,您可以使用:

select count(*) TotalClients
from yourtable
where dt >= (to_date('2004-05-20', 'yyyy-mm-dd') - INTERVAL '14' DAY)
  and dt <= (to_date('2004-05-20', 'yyyy-mm-dd') + INTERVAL '14' DAY)

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

Based on some of your previous questions you were using MySQL. 基于您以前的一些问题,您使用的是MySQL。

If you are using MySQL then you can use the DATE_ADD() function to get the date range and then use count(*) to return all records from those dates: 如果您使用的是MySQL,则可以使用 DATE_ADD()函数获取日期范围,然后使用 count(*)返回这些日期的所有记录:

 
 
 
 
  
  
  select count(*) TotalClients from yourtable where dt >= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL -14 day) and dt <= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL 14 day)
 
 
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM