title: Tag everytime a member appears again as circulated
body: <p>I want to tag circulations, that is everytime a member appears again.
  This list shows the result I want</p>
  
  <pre><code>Sequence ID Aktivity ID From, Agent ID  To, Agent ID    Movement    Circulated
  1           001A9552444 YNGBAN          TOMYAN          1           0
  2           001A9552461 TOMYAN          THOKIN          1           0
  3           001A9552466 THOKIN          MATLOV          1           0
  4           001A9552486 MATLOV          THOKIN          1           1
  5           001A9552499 THOKIN          YNGBAN          1           1
  6           001A9554213 YNGBAN          IKJER           1           0
  </code></pre>
  
  <p>Code for creating base table</p>
  
  <pre><code>DROP TABLE IF EXISTS [dbo].[Basetable]
  CREATE TABLE [dbo].[Basetable] (
  [Incident ID] INT,
  [Activity ID] VARCHAR(50),
  [Agent ID] VARCHAR(50),
  )
  INSERT INTO [dbo].[Basetable]
  VALUES
  (1072326, '001A9552444', 'YNGBAN'),
  (1072326, '001A9552461', 'TOMYAN'),
  (1072326, '001A9552463', 'THOKIN'),
  (1072326, '001A9552464', 'THOKIN'),
  (1072326, '001A9552465', 'THOKIN'),
  (1072326, '001A9552466', 'THOKIN'),
  (1072326, '001A9552468', 'MATLOV'),
  (1072326, '001A9552484', 'MATLOV'),
  (1072326, '001A9552485', 'MATLOV'),
  (1072326, '001A9552486', 'MATLOV'),
  (1072326, '001A9552499', 'THOKIN'),
  (1072326, '001A9554212', 'YNGBAN'),
  (1072326, '001A9554213', 'YNGBAN'),
  (1072326, '001A9568252', 'IKJER')
  </code></pre>
  
  <p>Below is the code so far. I don't want to do Group By and Count and have tried Left Join and Outer Apply but haven't found a good solution yet.</p>
  
  <pre><code>WITH X AS (
  SELECT
     ROW_NUMBER() OVER (ORDER BY [Activity ID]) AS [Sequence ID]
     ,[Incident ID]
     ,[Activity ID]
     ,[From, Agent ID]
     ,[To, Agent ID] 
     ,1 AS [Movement]
  FROM (
     SELECT
           [Incident ID]
           ,[Activity ID]
           ,[Agent ID] AS [From, Agent ID] 
           ,LEAD([Agent ID],1,0) OVER (PARTITION BY [Incident ID] ORDER BY [Activity ID]) AS [To, Agent ID] 
     FROM [dbo].[Basetable]
     ) AS X
  WHERE
     [To, Agent ID] != '0' AND [From, Agent ID] != [To, Agent ID]
  )
  SELECT
     X1.[Sequence ID]
     ,X1.[Incident ID]
     ,X1.[Activity ID]
     ,X1.[From, Agent ID]
     ,X1.[To, Agent ID] 
     ,X1.[Movement]
  FROM X AS X1
  </code></pre>
  
  <p>Edit: added code for creating base table and cleaned up the main code.</p>