title: SQL - sum data for all time, 30 days and 90 days for multiple columns indiviually
body: <p><strong>BACKGROUND:</strong></p>
  
  <p>I have data that looks like this</p>
  
  <pre><code>date        src    subsrc   subsubsrc   param1  param2
  2020-02-01  src1    ksjd    dfd8        47      31    
  2020-02-02  src1    djsk    zmnc        44      95    
  2020-02-03  src2    skdj    awes        92      100   
  2020-02-04  src2    mxsf    kajs        80      2     
  2020-02-05  src3    skdj    asio        46      53    
  2020-02-06  src3    dekl    jdqo        19      18    
  2020-02-07  src3    dskl    dqqq        69      18    
  2020-02-08  src4    sqip    riow        64      46    
  2020-02-09  src5    ss01    qwep        34      34    
  </code></pre>
  
  <p>I am trying to aggregate for all time, last 30 days and last 90 days (no rolling sum)</p>
  
  <p>So my final data would look like this:</p>
  
  <pre><code>src     subsrc  subsubsrc   p1_all  p1_30   p1_90   p2_all  p2_30   p2_90
  src1    ksjd    dfd8        7       1       7       98      7        98
  src1    djsk    zmnc        0       0       0       0       0         0
  src2    skdj    awes        12      12      12      4       4         4
  src2    mxsf    kajs        6       6       6       31      31       31
  src3    skdj    asio        0       0       0       0       0         0
  src3    dekl    jdqo        20      20      20      17      17        17
  src3    dskl    dqqq        3       3       3       4       4         4
  src4    sqip    qwep        0       0       0       0       0         0
  src5    ss01    qwes        15      15      15      2       2         2
  </code></pre>
  
  <p><strong>ABOUT DATA:</strong></p>
  
  <ul>
  <li>This is only dummy data and therefore incorrect.</li>
  <li>There are tens of thousands of rows in my data.</li>
  <li>There are a dozen of src columns that make up the key for the table.</li>
  <li>There are a dozen of param columns that I have to sum for 30 and 90 and all time.</li>
  <li>Also there are null values in param columns.</li>
  <li>Also there are might be multiple rows for same day and src column.</li>
  <li>New data is being added every day and the query is probably going to be run every day to get the latest 30, 90, all time data.</li>
  </ul>
  
  <p><strong>WHAT I HAVE TRIED:</strong></p>
  
  <p>This is what I have come up with:</p>
  
  <pre><code>SELECT src, subsubsrc, subsubsrc,
  SUM(param1) as param1_all,
  SUM(CASE WHEN DATE_DIFF(CURRENT_DATE,date,day) &lt;= 30 THEN param1 END) as param1_30,
  SUM(CASE WHEN DATE_DIFF(CURRENT_DATE,date,day) &lt;= 90 THEN param1 END) as param1_90,
  SUM(param2) as param2_all,
  SUM(CASE WHEN DATE_DIFF(CURRENT_DATE,date,day) &lt;= 30 THEN param2 END) as param2_30,
  SUM(CASE WHEN DATE_DIFF(CURRENT_DATE,date,day) &lt;= 90 THEN param2 END) as param2_90,
  FROM `MY_TABLE`
  GROUP BY src
  ORDER BY src
  </code></pre>
  
  <p>This actually works but I can anticipate how long this query is going to become for multiple sources and even more param columns. </p>
  
  <p>I have been trying something called "<strong>Filtered aggregate functions (or manual pivot)</strong>" explained <a href="https://blog.jooq.org/2017/04/20/how-to-calculate-multiple-aggregate-functions-in-a-single-query/" rel="nofollow noreferrer">HERE</a>. But I am unable to understand/implement it for my case.</p>
  
  <p>Also  I have looked at dozens of answers and most of them are running sums for each day OR are complicated cases of this basic calculation. Maybe I am not searching it correctly.</p>
  
  <p>As you can see I am newbie in SQL and would really appreciate any help.</p>