I have a problem that I feel could be solved using lag/lead + partitions but I can't wrap my head around it.
Clients are invited to participate in research-projects every two years (aprox.). A number of clients is selected for each project. Some clients are selected for multiple research-projects. Those get sent an invitation. In some cases no invitation is sent. If a client does not react to an invitation, a 2nd invitation (reminder) is sent. A 3rd, a 4rd are also possible.
I need to find out whether a client has had an invitation for a previous research-project. (And optionally, which invitation that was).
The dataset looks like this:
clientID | projectID | invitationID
14 | 267 | 489
14 | 267 | 325
16 | 385 | 475
17 | 546 | NULL
17 | 547 | 885
17 | 548 | 901
18 | 721 | 905
18 | 834 | 906
18 | 834 | 907
19 | 856 | 908
19 | 856 | 929
19 | 857 | 931
19 | 857 | 945
19 | 858 | NULL
Client 14 has had 2 invitations for the same research-project
Client 16 has had 1 invitation for 1 research-project
Client 17 has been selected for 3 research-projects but opted out for project 546, receiving 1 invitation each for the following projects.
Client 18 has been selected for 2 research-projects. For the second project he got a 2 invitations.
Client 19 has been selected for three research-projects. For the first two a reminder was set. Client 19 was selected for project 858 but opted out thus no invitation.
Now I need to determine per client whether there has been a invitation for a previous research-project. (And optionally, which invitation that was). I only need the first invitation (if there were multiple). So my resulting dataset should look like this (stuff between brackets is optional):
clientID | projectID | invitationID | InvitedForPreviousProject
14 | 267 | 489 | 0
14 | 267 | 325 | 0
16 | 385 | 475 | 0
17 | 546 | NULL | 0
17 | 547 | 885 | 0
17 | 548 | 901 | 1 (885)
18 | 721 | 905 | 0
18 | 834 | 906 | 1 (905)
18 | 834 | 907 | 1 (905)
19 | 856 | 908 | 0
19 | 856 | 929 | 0
19 | 857 | 931 | 1 (908)
19 | 857 | 945 | 1 (908)
19 | 858 | NULL | 1 (931)
Can this be done using LEAD, Rank, Dense-Rank? Create-statement including data below
declare @table table (
[clientID] [int] NULL,
[projectID] [int] NULL,
[invitationID] [int] NULL
)
INSERT @table ([clientID], [projectID], [invitationID]) VALUES
(14, 267, 489),
(14, 267, 325),
(16, 385, 475),
(17, 546, NULL),
(17, 547, 885),
(17, 548, 901),
(18, 721, 905),
(18, 834, 906),
(18, 834, 907),
(19, 856, 908),
(19, 856, 929),
(19, 857, 931),
(19, 857, 945),
(19, 858, NULL)