title: Query without WHILE Loop
body: <p>We have appointment table as shown below. Each appointment need to be categorized as "New" or "Followup". Any appointment (for a patient) within 30 days of first appointment (of that patient) is Followup.  After 30 days, appointment is again "New". Any appointment within 30 days become "Followup".</p>
  
  <p>I am currently doing this by typing while loop.<br>
  How to achieve this without WHILE loop?</p>
  
  <p><a href="https://i.stack.imgur.com/nDTnT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nDTnT.png" alt="enter image description here"></a></p>
  
  <p>Table</p>
  
  <pre><code>CREATE TABLE #Appt1 (ApptID INT, PatientID INT, ApptDate DATE)
  INSERT INTO #Appt1
  SELECT  1,101,'2020-01-05' UNION
  SELECT  2,505,'2020-01-06' UNION
  SELECT  3,505,'2020-01-10' UNION
  SELECT  4,505,'2020-01-20' UNION
  SELECT  5,101,'2020-01-25' UNION
  SELECT  6,101,'2020-02-12'  UNION
  SELECT  7,101,'2020-02-20'  UNION
  SELECT  8,101,'2020-03-30'  UNION
  SELECT  9,303,'2020-01-28' UNION
  SELECT  10,303,'2020-02-02' 
  </code></pre>