Note that there are some explanatory texts on larger screens.

plurals
  1. POMYSQL MULTIPLE COUNT AND SUM
    primarykey
    data
    text
    <p>I hope this is possible in MYSQL, I am scripting with PHP.</p> <p>I am trying to create multiple column on SUM of values and COUNT on table1 based on each month based with individual conditions and groupings. The tables are already joined through the accountid. I have two tables monthlyreport(table1) &amp; planters(table2).</p> <p>Desired Results is in table 1</p> <p>MONTHLY REPORT (Table 1)</p> <pre><code>REPORTID|ACCOUNTID|COMPMONTH|SUMtoDATE|COUNTtoDATE|SUMcompDATE|COUNTcompDATE| 1 | 190 | JAN | 150 | 2 | 150 | 2 | 2 | 190 | FEB | 0 | 0 | 100 | 1 | </code></pre> <p>Planters (Table 2)</p> <pre><code>PlanterID | ACCOUNTID |PLANTER | SALARY | compDATE | toDATE | 1 | 190 | aaa | 100 | Jan-1-2013 | Jan-05-2013 | 2 | 190 | bbb | 50 | Jan-9-2013 | Jan-12-2013 | 3 | 190 | aaa | 100 | Feb-1-2013 | Mar-12-2013 | 4 | 190 | bbb | 0 | Mar-5-2013 | Mar-12-2013 | </code></pre> <p>A single query with inner join already works but if I run both I get nothing because I can't seem to get the logic if it is possible.</p> <p>This is what I have so far from stackoverflow but getting error. Wish someone can refactor it or make it work.</p> <pre><code>SELECT *, ( SELECT COUNT(planters.todate), SUM(planters.todate) FROM monthlyreport INNER JOIN planters ON monthlyreport.accountid = planters.accountid WHERE monthlyreport.accountid = 190 AND MONTH(monthlyreport.compmonth) = MONTH(planters.todate) GROUP BY monthlyreport.mthreportid, month(planters.todate) ) AS count_1, ( SELECT COUNT(planters.compdate), SUM(planters.compdate) FROM monthlyreport INNER JOIN planters ON monthlyreport.accountid = planters.accountid WHERE monthlyreport.accountid = 190 AND MONTH(monthlyreport.compmonth) = MONTH(planters.compdate) GROUP BY monthlyreport.mthreportid, month(planters.compdate) ) AS count_2 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload