找出第一次出现和随后出现的情况

时间:2017-03-05 09:07:23

标签: sql sql-server tsql

大家好我所有下面的表格,我想回报一下2014年第一次来的人,他们在第一次访问后2个月以及第一次访问后不到13个月内进行了后续访问。 / p>

ID  firstname   surname dob event_key   event_date  status
1   Mark    Zeus    12-Dec-67   10  02-Feb-14   Appointment
1   Mark    Zeus    12-Dec-67   10  02-Feb-14   Attend
1   Mark    Zeus    12-Dec-67   11  06-Jul-14   Appointment
1   Mark    Zeus    12-Dec-67   11  06-Jul-14   Attend
1   Mark    Zeus    12-Dec-67   12  23-Nov-14   Appointment
1   Mark    Zeus    12-Dec-67   12  23-Nov-14   Attend
1   Mark    Zeus    12-Dec-67   13  23-Nov-14   Appointment
1   Mark    Zeus    12-Dec-67   13  23-Nov-14   Attend
2   Jane    Adams   24-Jul-57   14  15-Mar-15   Appointment
2   Jane    Adams   24-Jul-57   14  15-Mar-15   Attend
2   Jane    Adams   24-Jul-57   15  20-Apr-14   Appointment
2   Jane    Adams   24-Jul-57   15  20-Apr-14   Attend
2   Jane    Adams   24-Jul-57   16  04-Jan-15   Appointment
2   Jane    Adams   24-Jul-57   16  04-Jan-15   Attend
3   Rohan   Clark   05-Jan-78   17  22-Apr-09   Appointment
3   Rohan   Clark   05-Jan-78   17  22-Apr-09   Attend
3   Rohan   Clark   05-Jan-78   19  21-Mar-14   Appointment
3   Rohan   Clark   05-Jan-78   19  21-Mar-14   Attend
3   Rohan   Clark   05-Jan-78   20  30-Jun-15   Appointment
3   Rohan   Clark   05-Jan-78   20  30-Jun-15   Attend
3   Rohan   Clark   05-Jan-78   21  01-Jan-16   Appointment
3   Rohan   Clark   05-Jan-78   21  01-Jan-16   Attend
4   James   Gohan   03-Jun-78   22  17-Aug-14   Appointment
4   James   Gohan   03-Jun-78   22  17-Aug-14   Attend
4   James   Gohan   03-Jun-78   23  30-Dec-14   Appointment
4   James   Gohan   03-Jun-78   23  30-Dec-14   Attend
4   James   Gohan   03-Jun-78   24  03-Mar-15   Appointment
4   James   Gohan   03-Jun-78   24  03-Mar-15   Attend
4   James   Gohan   03-Jun-78   25  12-Apr-16   Appointment
4   James   Gohan   03-Jun-78   25  12-Apr-16   Attend
5   Isabel  Robyn   07-Jul-56   26  07-Jul-15   Appointment
5   Isabel  Robyn   07-Jul-56   26  07-Jul-15   Attend
5   Isabel  Robyn   07-Jul-56   27  12-May-16   Appointment
5   Isabel  Robyn   07-Jul-56   27  12-May-16   Attend
5   Isabel  Robyn   07-Jul-56   28  18-Feb-17   Appointment
5   Isabel  Robyn   07-Jul-56   28  18-Feb-17   Attend

以下是我的查询无效

SELECT              *

FROM                #temp   t1

WHERE               t1.[event_date] BETWEEN '01-Jan-2014' AND '31-Dec-2016'

                    AND t1.[ID]                 
                    IN
                        (
                            SELECT DISTINCT t2.[ID]
                            FROM #temp t2
                            WHERE   t2.[event_date] <= DATEADD(month, 13, t2.[event_date]) AND t2.[event_date] >= DATEADD(month, 2, [event_date])
                        )

ORDER BY            t1.[ID], t1.[event_key]

-- DROP TABLE           #temp

2 个答案:

答案 0 :(得分:0)

您可以拥有一个嵌套表来返回所有第一次访问的人,并将其他访问与之比较。

select distinct t1.id from Table1 t1 inner join 
    (select id, min(event_date) firstVisit from Table1 
    group by id
    having min(event_date) between '2014-01-01' and '2014-12-31') t2
    on t2.id=t1.id
where DATEDIFF(MONTH,t2.firstVisit,t1.event_date) > 2 
    and DATEDIFF(MONTH,t2.firstVisit,t1.event_date) < 13

这将回馈:

1
2
4

答案 1 :(得分:0)

我会使用条件聚合来做到这一点:

foo