简体   繁体   中英

MYSQL MULTIPLE COUNT AND SUM

I hope this is possible in MYSQL, I am scripting with PHP.

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) & planters(table2).

Desired Results is in table 1

MONTHLY REPORT (Table 1)

REPORTID|ACCOUNTID|COMPMONTH|SUMtoDATE|COUNTtoDATE|SUMcompDATE|COUNTcompDATE|
1     |   190     |    JAN    |   150     |      2      |    150      |       2       | 
2     |   190     |    FEB    |     0     |      0      |    100      |       1       |

Planters (Table 2)

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 |

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.

This is what I have so far from stackoverflow but getting error. Wish someone can refactor it or make it work.

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

Its not very clear, but as far as I can think, what you want is to get the two results in a single query result. Try joining them on the basis of accountID from both the tables.AS:

SELECT *
from
(select accountID,COUNT(planters.todate) as count2date, SUM(planters.todate) as sum2date
-----
-----) count_1
inner join
(SELECT accountID,COUNT(planters.compdate) as countcomp, SUM(planters.compdate) as sumcomp
-----
-----) count_2
using(accountID);

Do not use "AS" before count_1 or count_2. It is better to replace * in the outer select query with more specific attributes, like count_1.count2date or like.

Hope this helps ! If anything else is what you are looking for, do let me know.

-----UPDATE-----

After looking at your file you uploaded, I came up with the following query:

SELECT count1.compmonth, IFNULL( todatecount, 0 ) , IFNULL( todatesum, 0 ) , IFNULL(      compdatecount, 0 ) , IFNULL( compdatesum, 0 ) 
FROM count_1
LEFT JOIN count_2 ON count_1.compmonth = count_2.compmonth
UNION 
SELECT count2.compmonth, IFNULL( todatecount, 0 ) , IFNULL( todatesum, 0 ) , IFNULL( compdatecount, 0 ) , IFNULL( compdatesum, 0 ) 
FROM count_1
RIGHT JOIN count_2 ON count_1.compmonth = count_2.compmonth

You can format the 0's as per your wish. Also, if your database platform supports the "FULL OUTER JOIN", you can use that instead of making a union of left and right joins.

You will have to replace "FROM count_1" with:
FROM (select accountID,COUNT(planters.todate) as count2date, SUM(planters.todate) as sum2date ----- -----) count_1

Similarly for FROM count_2 . I know this looks like a huge query, but all this does is joins the 2 tables on common dates, and all the other fields that don't match are specified NULL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM