创建SQL查询以返回事件的最早日期和持续时间,直到发生不同的事件

创建SQL查询以返回事件的最早日期和持续时间,直到发生不同的事件,sql,date,ms-access,ado,duration,Sql,Date,Ms Access,Ado,Duration,我正在尝试编写一个SQL查询(使用ADO从Excel查询访问权限),以返回每个不同的用户ID,以及EventID=916的最早的日期和该用户在916事件后发生的EventID=944的最早的日期。此外,我想计算两个日期之间的持续时间(以天为单位) 试验数据如下: create table table_name ( EventID int, UserId int, MsgVar1 varchar(4), MsgVar2 varchar(4), EventDate date );

我正在尝试编写一个SQL查询(使用ADO从Excel查询访问权限),以返回每个不同的用户ID,以及EventID=916的最早的日期和该用户在916事件后发生的EventID=944的最早的日期。此外,我想计算两个日期之间的持续时间(以天为单位)

试验数据如下:

create table table_name
(
  EventID int,
  UserId int,
  MsgVar1 varchar(4),
  MsgVar2 varchar(4),
  EventDate date
);

insert into table_name (EventId,UserId,MsgVar1,MsgVar2,EventDate) values 
(916,123456,'x', 'x','20110920'),
(944,123456,'x','x','20110923'),
(945,123456,'x','x','20110925'),
(916,123456,'x', 'x','20110928'),
(944,123456,'x', 'x','20110928'),
(916,123458,'x', 'x','20110919'),
(944,123458,'x','x','20110928');
查询应返回以下内容:

UserId | Event916Date | Event944Date | Duration
-----------------------------------------------
123456 | 20110920     | 20110923     | 3
123458 | 20110919     | 20110928     | 9
insert into table_name (EventId,UserId,MsgVar1,MsgVar2,EventDate) values
(944,123456,'x','x','20110910'),
(916,123456,'x','x','20110920'),
(944,123456,'x','x','20110923'),
(945,123456,'x','x','20110925'),
(944,123456,'x','x','20110928'),
(916,123458,'x','x','20110919'),
(944,123458,'x','x','20110928');
我的出发点如下,但是现在它返回所有944个事件,而不仅仅是最古老的事件

select start.UserID, start.EventDate start, end.EventDate end, datediff(end.EventDate, start.EventDate) duration
from (
    select *, (
        select UserID from table_name L2 where L2.EventDate>L1.EventDate and L2.UserId=L1.UserId order by EventDate limit 1
    ) stop_id from table_name L1
) start
join table_name end on end.UserID=start.stop_id
where start.EventID=916 and end.EventID=944;

测试数据与预期结果不匹配。仅从提供的样本数据来看,eventID 916的
最新日期为20110928,用户ID 123456,该日期之后该用户的
最早日期为20110928。我删除了用户123456的最后一个事件id 916,并能够获得预期的结果

我将您的测试数据更改为以下内容:

UserId | Event916Date | Event944Date | Duration
-----------------------------------------------
123456 | 20110920     | 20110923     | 3
123458 | 20110919     | 20110928     | 9
insert into table_name (EventId,UserId,MsgVar1,MsgVar2,EventDate) values
(944,123456,'x','x','20110910'),
(916,123456,'x','x','20110920'),
(944,123456,'x','x','20110923'),
(945,123456,'x','x','20110925'),
(944,123456,'x','x','20110928'),
(916,123458,'x','x','20110919'),
(944,123458,'x','x','20110928');
下面是我通过单个查询得到的最接近您的结果,但它不起作用,因为您不能在后续子查询中使用来自一个子查询的别名结果。也许其他人会给你一个问题的答案,因为这似乎是可行的

select t1.userID, 
(select max(eventdate) from table_name t2 where t1.userid = t2.userid and eventid = 916) as Event916Date
from table_Name t1, 
(select t3.userID, 
(select min(eventdate) from table_name t4 where t4.userid = t3.userid and eventid = 944 and eventDate >= t1.Event916Date) as Event944Date
from table_Name t3) as t2
group by t1.userID
在access中,我可以通过几个查询来实现这一点

T1:获取事件916和每个用户的最大事件日期

SELECT table_name.UserId, Max(table_name.EventDate) AS MaxOfEventDate
FROM table_name
GROUP BY table_name.UserId, table_name.EventID
HAVING (((table_name.EventID)=916));
SELECT t2.UserId, Min(t2.EventDate) AS MinOfEventDate
FROM t2
GROUP BY t2.UserId;
T2:获取944和每个用户的活动日期,这些日期大于您的最小值

SELECT t1.UserId, table_name.EventDate
FROM table_name INNER JOIN t1 ON table_name.UserId = t1.UserId
WHERE (((table_name.EventDate)>[maxofeventdate]) AND ((table_name.eventId)=944));
T3:获取每个用户的最小事件日期944

SELECT table_name.UserId, Max(table_name.EventDate) AS MaxOfEventDate
FROM table_name
GROUP BY table_name.UserId, table_name.EventID
HAVING (((table_name.EventID)=916));
SELECT t2.UserId, Min(t2.EventDate) AS MinOfEventDate
FROM t2
GROUP BY t2.UserId;
T4:最终查询以显示两者并计算持续时间

SELECT t1.UserId, t1.MaxOfEventDate, t3.MinOfEventDate, [MinOfEventDate]-[MaxOfEventDate] AS Duration
FROM t1 INNER JOIN t3 ON t1.UserId = t3.UserId;
如果您使用的是excel中的ADO,您应该可以访问querydef对象,以便能够创建和使用多个查询