如果我有一张大表(100000 +条),其中有服务记录或可能有录取记录。如何在设定的天数内找到所有重新发生的实例。
表格设置可能是这样的,可能有更多列。
Record ID Customer ID Start Date Time Finish Date Time
1 123456 24/04/2010 16:49 25/04/2010 13:37
3 654321 02/05/2010 12:45 03/05/2010 18:48
4 764352 24/03/2010 21:36 29/03/2010 14:24
9 123456 28/04/2010 13:49 31/04/2010 09:45
10 836472 19/03/2010 19:05 20/03/2010 14:48
11 123456 05/05/2010 11:26 06/05/2010 16:23
我想要做的是找出一种方法来选择在特定时间段( 这是我希望它一旦运行说x = 7 我可以使用Excel中较小的记录集解决此问题,但很难在MS Access中提出SQL解决方案。我确实有一些SQL查询,但我不确定自己是否在正确的轨道上。 任何建议都将不胜感激。 Record ID Customer ID Start Date Time Finish Date Time Re-occurence
9 123456 28/04/2010 13:49 31/04/2010 09:45 1
11 123456 05/05/2010 11:26 06/05/2010 16:23 2
答案 0 :(得分:3)
我认为这清楚地表达了你想要的东西。它不是非常高的性能,但我不确定你可以避免相关的子查询或表的笛卡尔JOIN来解决这个问题。它是标准的SQL,应该适用于大多数引擎,尽管日期数学的细节可能不同:
SELECT * FROM YourTable YT1 WHERE EXISTS
(SELECT * FROM YourTable YT2 WHERE
YT2.CustomerID = YT1.CustomerID AND YT2.StartTime <= YT2.FinishTime + 7)
答案 1 :(得分:0)
为了实现这一点,您需要在将整个表与自身进行比较时进行自联接。假设名称相似,它看起来像这样:
select r1.customer_id, min(start_time), max(end_time), count(1) as reoccurences
from records r1,
records r2
where r1.record_id > r2.record_id -- this ensures you don't double count the records
and r1.customer_id = r2.customer_id
and r1.finish_time - r2.start_time <= 7
group by r1.customer_id
您将无法轻松获取record_id和出现次数,但您可以通过将开始时间与记录号码与customer_id和start_time相关联来返回并找到它。
答案 2 :(得分:0)
这样做:
declare @t table(Record_ID int, Customer_ID int, StartDateTime datetime, FinishDateTime datetime)
insert @t values(1 ,123456,'2010-04-24 16:49','2010-04-25 13:37')
insert @t values(3 ,654321,'2010-05-02 12:45','2010-05-03 18:48')
insert @t values(4 ,764352,'2010-03-24 21:36','2010-03-29 14:24')
insert @t values(9 ,123456,'2010-04-28 13:49','2010-04-30 09:45')
insert @t values(10,836472,'2010-03-19 19:05','2010-03-20 14:48')
insert @t values(11,123456,'2010-05-05 11:26','2010-05-06 16:23')
declare @days int
set @days = 7
;with a as (
select record_id, customer_id, startdatetime, finishdatetime,
rn = row_number() over (partition by customer_id order by startdatetime asc)
from @t),
b as (
select record_id, customer_id, startdatetime, finishdatetime, rn, 0 recurrence
from a
where rn = 1
union all
select a.record_id, a.customer_id, a.startdatetime, a.finishdatetime,
a.rn, case when a.startdatetime - @days < b.finishdatetime then recurrence + 1 else 0 end
from b join a
on b.rn = a.rn - 1 and b.customer_id = a.customer_id
)
select record_id, customer_id, startdatetime, recurrence from b
where recurrence > 0
结果: http://data.stackexchange.com/stackoverflow/q/112808/
我只是意识到它应该在访问中完成。我很抱歉,这是为sql server 2005编写的。我不知道如何重写它以进行访问。