title: SQL Query for summing up values from rows within specific time period (within ID)?
body: <p>I need to sum up values (Amount2) from rows within a specific time period (datediff 18hours or 22hours) and also need to know how many rows were used. This summing the values has to be done within the ID number. Can anyone help with this?</p>
  
  <p>First rows of the Table T1:         </p>
  
  <pre><code>ID  PickupDateTime          Amount2 DifferenceToPrevious
  1   2019-04-02 04:44:19.000 6458    0d 00:13:17:000
  1   2019-04-02 04:31:02.000 5385    0d 02:34:50:000
  1   2019-04-02 01:56:12.000 1545    0d 18:06:18:000
  1   2019-04-01 07:49:54.000 3466    1d 02:23:51:000
  1   2019-03-31 05:26:03.000 7505    0d 00:13:53:000
  1   2019-03-31 05:12:10.000 5080    0d 03:28:18:000
  1   2019-03-31 01:43:52.000 1166    0d 18:02:49:000
  1   2019-03-30 07:41:03.000 2991    1d 02:39:49:000
  1   2019-03-29 05:01:14.000 7065    0d 00:13:11:000
  2   2019-04-01 04:56:21.000 9518    1d 23:52:21:000
  2   2019-03-30 05:04:00.000 9638    2d 00:34:29:000
  2   2019-03-28 04:29:31.000 9499    1d 23:28:47:000
  2   2019-03-26 05:00:44.000 10117   2d 00:03:41:000
  2   2019-03-24 04:57:03.000 9933    1d 23:50:06:000
  2   2019-03-22 05:06:57.000 9869    2d 00:25:01:000
  </code></pre>
  
  <p>What I need is script that sums Amount2 from rows where PickupDate time is within 18 hours or 22hours, calculates also the rows which are summed (this has to be done within ID).                               </p>
  
  <pre><code>PickupDateTime  Amount2 DifferenceToPrevious    Amount2_18h Rows_18 Amount2_22h Rows_22
  2019-04-02 04:44:19.000 6458    0d 00:13:17:000 13388   3   16854   4
  2019-04-02 04:31:02.000 5385    0d 02:34:50:000 6930    2   10396   3
  2019-04-02 01:56:12.000 1545    0d 18:06:18:000 1545    1   12516   3
  2019-04-01 07:49:54.000 3466    1d 02:23:51:000 3466    1   3466    1
  2019-03-31 05:26:03.000 7505    0d 00:13:53:000 13751   3   16742   4
  2019-03-31 05:12:10.000 5080    0d 03:28:18:000 6246    2   9237    3
  2019-03-31 01:43:52.000 1166    0d 18:02:49:000 1166    1   4157    2
  2019-03-30 07:41:03.000 2991    1d 02:39:49:000 2991    1   2991    1
  2019-04-01 04:56:21.000 9518    1d 23:52:21:000 9518    1   9518    1
  2019-03-30 05:04:00.000 9638    2d 00:34:29:000 9638    1   9638    1
  2019-03-28 04:29:31.000 9499    1d 23:28:47:000 9499    1   9499    1
  2019-03-26 05:00:44.000 10117   2d 00:03:41:000 10117   1   10117   1
  2019-03-24 04:57:03.000 9933    1d 23:50:06:000 9933    1   9933    1
  2019-03-22 05:06:57.000 9869    2d 00:25:01:000 9869    1   9869    1
  </code></pre>