简体   繁体   English

MYSQL多个计数和总和

[英]MYSQL MULTIPLE COUNT AND SUM

I hope this is possible in MYSQL, I am scripting with PHP. 我希望这在MYSQL中是可行的,我正在使用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. 我正在尝试根据每个月的具体条件和分组,在值的总和和table1的COUNT上创建多个列。 The tables are already joined through the accountid. 这些表已通过accountid联接。 I have two tables monthlyreport(table1) & planters(table2). 我有两个表monthlyreport(table1)和planters(table2)。

Desired Results is in table 1 所需结果在表1中

MONTHLY REPORT (Table 1) 每月报告(表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) 播种机(表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. 这是我到目前为止与stackoverflow无关的错误。 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: 尝试根据两个表中的accountID加入它们:

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. 在count_1或count_2之前不要使用“ AS”。 It is better to replace * in the outer select query with more specific attributes, like count_1.count2date or like. 最好将外部选择查询中的*替换为更具体的属性,例如count_1.count2date等。

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. 您可以根据需要设置0的格式。 Also, if your database platform supports the "FULL OUTER JOIN", you can use that instead of making a union of left and right joins. 另外,如果您的数据库平台支持“ FULL OUTER JOIN”,则可以使用它来代替左右联接的并集。

You will have to replace "FROM count_1" with: 您必须将“ FROM count_1”替换为:
FROM (select accountID,COUNT(planters.todate) as count2date, SUM(planters.todate) as sum2date ----- -----) count_1

Similarly for FROM count_2 . 对于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. 我知道这看起来像一个巨大的查询,但是所有这些都是在共同的日期联接2个表,而所有其他不匹配的字段都被指定为NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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