title: How can I create a loop statement in BigQuery and add difference in days since the start-records for each record in the same loop?
body: <p>I want to create a table with a select statement, that for each record where &quot;Type = Start&quot; takes all the records that have the same &quot;Domain&quot; as the Start-record AND have a timestamp that is the same as the Start-record or up to Two years after.</p>
  <p>Also if there is another Start-record in this period that also have the same Domain. In those cases the same record can be added multiple times.</p>
  <p>I also want to add a field &quot;Days&quot; that contain the number of days since the Start-record.</p>
  <p>I want an additional id &quot;New_Id&quot; that has the same id as the original Start-record for all the records in the Start/domain/two-years-range.</p>
  <p>I have a table:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Id</th>
  <th>Domain</th>
  <th>Type</th>
  <th>Timestamp</th>
  <th>coll1</th>
  <th>coll2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>400</td>
  <td>domain3</td>
  <td><strong>Start</strong></td>
  <td>2021-05-20T09:26:49</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-02-14T12:16:32</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>100</td>
  <td>domain1</td>
  <td><strong>Start</strong></td>
  <td>2021-04-13T03:23:11</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>4</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-02-14T12:16:32</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-05-10T13:16:35</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>200</td>
  <td>domain2</td>
  <td><strong>Start</strong></td>
  <td>2021-04-19T07:10:08</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-17T10:30:40</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>8</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-05-14T12:47:42</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>1</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-17T09:26:45</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>3</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-04-13T03:23:11</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>3</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-06-13T16:56:51</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>2</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-20T09:26:49</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>3</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-01T13:17:31</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>9</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-13T16:56:51</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-17T10:30:40</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-05-10T13:16:35</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-04-19T07:10:08</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>1</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-14T12:47:42</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>domain3</td>
  <td><strong>Start</strong></td>
  <td>2021-05-13T08:40:04</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>8</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-05-17T09:26:45</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-05-01T13:17:31</td>
  <td>content</td>
  <td>content</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>And I want to end with a new table like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>New_Id</th>
  <th>Id</th>
  <th>Domain</th>
  <th>Type</th>
  <th>Timestamp</th>
  <th>Days</th>
  <th>coll1</th>
  <th>coll2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>100</td>
  <td>100</td>
  <td>domain1</td>
  <td><strong>Start</strong></td>
  <td>2021-04-13T03:23:11</td>
  <td>0</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>100</td>
  <td>3</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-01T13:17:31</td>
  <td>18</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>100</td>
  <td>1</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-14T12:47:42</td>
  <td>31</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>100</td>
  <td>1</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-17T09:26:45</td>
  <td>34</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>100</td>
  <td>2</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-05-20T09:26:49</td>
  <td>37</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>100</td>
  <td>3</td>
  <td>domain1</td>
  <td>Other</td>
  <td>2021-06-13T16:56:51</td>
  <td>61</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>200</td>
  <td>200</td>
  <td>domain2</td>
  <td><strong>Start</strong></td>
  <td>2021-04-19T07:10:08</td>
  <td>0</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>200</td>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-04-19T07:10:08</td>
  <td>0</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>200</td>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-05-01T13:17:31</td>
  <td>12</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>200</td>
  <td>5</td>
  <td>domain2</td>
  <td>Other</td>
  <td>2021-05-10T13:16:35</td>
  <td>21</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>300</td>
  <td>domain3</td>
  <td><strong>Start</strong></td>
  <td>2021-05-13T08:40:04</td>
  <td>0</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>8</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-05-14T12:47:42</td>
  <td>1</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>8</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-05-17T09:26:45</td>
  <td>4</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>9</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-13T16:56:51</td>
  <td>31</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-17T10:30:40</td>
  <td>35</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>300</td>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-17T10:30:40</td>
  <td>35</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>400</td>
  <td>400</td>
  <td>domain3</td>
  <td><strong>Start</strong></td>
  <td>2021-05-20T09:26:49</td>
  <td>0</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>400</td>
  <td>9</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-13T16:56:51</td>
  <td>24</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>400</td>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-17T10:30:40</td>
  <td>28</td>
  <td>content</td>
  <td>content</td>
  </tr>
  <tr>
  <td>400</td>
  <td>7</td>
  <td>domain3</td>
  <td>Other</td>
  <td>2021-06-17T10:30:40</td>
  <td>28</td>
  <td>content</td>
  <td>content</td>
  </tr>
  </tbody>
  </table>
  </div>