title: SQL: Iterate through subquery
body: <p>I'm currently working on a query that flags all the orders that don't have the same email address as any other order within the last 10 days.</p>
  
  <p>Now I can check if the last order is less than 10 days old or not but if there are 3 orders:</p>
  
  <pre><code>1. 2019/01/01
  2. 2019/01/20
  3. 2019/01/22
  </code></pre>
  
  <p>Then all of them will get a yes because nr 3 will check nr 1. 
  So I need to build a loop inside the subquery to iterate through all the ones with the same email address. Please help:)</p>
  
  <pre><code>SELECT TOP (1000)       
          OnlyOneWithin10Days =  
                  CASE   
                  WHEN (select top (1) inner.CreationDate 
                      from [Database].[req].[PersonalDetail] as pd1 
                      JOIN req.OfferRequest ofreq on ofreq.MainDriverPersonalDetailID = inner.ID
                      JOIN rsp.InsuranceResponse inresp on ofreq.InsuranceResponseID = inresp.id
                      JOIN adm.ProductBundle inner2 on inresp.ProductBundleID = inner2.ID
                      where inner.Email = personalDetail.Email 
                      and inner2.ID = pb.ID
                      and personalDetail.id &gt; inner.ID
                      and inner.CreationDate &lt; personalDetail.CreationDate
                      order by inner.CreationDate desc) is NULL
  
                  OR  datediff(MINUTE, (select top (1) inner.CreationDate 
                      from [Database].[req].[PersonalDetail] as inner 
                      JOIN req.OfferRequest ofreq on ofreq.MainDriverPersonalDetailID = inner.ID
                      JOIN rsp.InsuranceResponse inresp on ofreq.InsuranceResponseID = inresp.id
                      JOIN adm.ProductBundle inner2 on inresp.ProductBundleID = inner2.ID
                      where inner.Email = personalDetail.Email 
                      and inner2.ID = pb.ID
                      and personalDetail.id &gt; inner.ID
                      and inner.CreationDate &lt; personalDetail.CreationDate
                      --and datediff(MINUTE, inner.CreationDate, personalDetail.CreationDate) &gt;= 10
                      order by inner.CreationDate desc), personalDetail.CreationDate) &gt;= 10
  
                  THEN 'Yes'
                  ELSE 'No'
                  END 
          ,personalDetail.CreationDate
          ,pb.Name product
          ,personalDetail.Name
          ,personalDetail.email
          ,i.Name insurance
  
    FROM [Database].[req].[OfferRequest] as oreq
  
    JOIN req.PersonalDetail personalDetail on oreq.MainDriverPersonalDetailID = personalDetail.ID
    JOIN rsp.InsuranceResponse ir on oreq.InsuranceResponseID = ir.ID
    JOIN adm.ProductBundle pb on ir.ProductBundleID = pb.ID
    JOIN adm.Insurance i on pb.InsuranceID = i.ID
  
  
    order by personalDetail.Email, personalDetail.CreationDate
  </code></pre>
  
  <p>Edit: I`m using SSMS (Microsoft SQL Server Management Studio    14.0.17289.0)</p>
  
  <p>Edit2: Sorry if my question is a bit confusing but I`m new to stackOverflow.</p>
  
  <p>The result should look something like this</p>
  
  <p><a href="https://i.stack.imgur.com/84n8Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/84n8Y.png" alt="enter image description here"></a></p>