title: MySQL Sum(if)) grouping
body: <p>I have the following unloaded table:
  payment_date,
  user_id,
  monthlyAmount_last30d</p>
  <p>user id and payment date are taken from the table. And in the <code>monthlyAmount_last30d field</code>, I want to see the amount for the period (payment date - 30 days).</p>
  <p>I can't figure out how to group the columns correctly for such a result. If I group them only by <code>user_id</code>, then I see only one first row with the payment date (I need all the rows, each of which will have the <em><strong>sum of payments for this user_id for the period (the payment date in the row - 30 days</strong></em>).
  And if I group by both fields -&gt; I get the payment amount always equal to the payment in this line, and not the real amount for the period I need.</p>
  <pre><code>select
  order_id,
  order_date,
  sum( if( ( order_date &lt;= order_date 
           and order_date &gt;= order_date - interval 30 day), order_cost_rub, 0)) monthlyAmount_last30d,
  user_id,
  order_cost_rub
  
  from MyTable
  
  group by
  user_id
  </code></pre>
  <p>Thanks for any tips!</p>
  <p>UPD:</p>
  <p>Data sample:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">user_id</th>
  <th style="text-align: center;">payment_date</th>
  <th style="text-align: right;">order_cost_rub</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">2022-01-01 12:00:00</td>
  <td style="text-align: right;">100</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">2022-01-01 13:00:00</td>
  <td style="text-align: right;">100</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">2022-01-03 20:00:00</td>
  <td style="text-align: right;">150</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">2022-01-03 16:00:00</td>
  <td style="text-align: right;">200</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">2022-01-15 11:00:00</td>
  <td style="text-align: right;">300</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>expected result:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">user_id</th>
  <th style="text-align: center;">payment_date</th>
  <th style="text-align: right;">order_cost_rub</th>
  <th style="text-align: right;">monthlyAmount_last30d</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">2022-01-01 12:00:00</td>
  <td style="text-align: right;">100</td>
  <td style="text-align: right;">100</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">2022-01-01  13:00:00</td>
  <td style="text-align: right;">100</td>
  <td style="text-align: right;">200</td>
  </tr>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">2022-01-03 20:00:00</td>
  <td style="text-align: right;">150</td>
  <td style="text-align: right;">350</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">2022-01-03 16:00:00</td>
  <td style="text-align: right;">200</td>
  <td style="text-align: right;">200</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">2022-01-15 11:00:00</td>
  <td style="text-align: right;">300</td>
  <td style="text-align: right;">500</td>
  </tr>
  </tbody>
  </table>
  </div>