如何根据不同的标准计算两个 MySQL SUM
How to do two MySQL SUMs based on different criterias
我有 6 个 table 彼此相关。我有一个收集所有数据的 JOIN 查询。我首先提供 cpv_id 作为来自用户的变量,然后最后我需要获取与该 cpv_id 相关的每个公司的报告(通过许多 table 之间他们 ).
基本上,流程是这样的:
我从用户那里得到 cpv_id,我在 club_offer_cpv table 中找到它并且我得到 club_offer_id,所以我可以连接到 club_offer table。然后在 club_offer table 我得到 club_id 所以我可以连接到 club table。然后在 club table 我得到 users_id 所以我可以连接到 users table。然后在 users table 我得到 company_id 所以我可以连接到 company table。在 company table 我得到 special_id 所以我可以连接到最后一个 table company_reports。这是我需要从中获取数据的地方。我需要这个:
这里我有3个兴趣领域:report_year、variable_code和 variable_value.
report_year 存储公司报告的年份。
variable_code 存储所做报告的类型。在我关于 SQL Fiddle 的简化示例中,可能的值是 AOP 605 和 AOP 201.
variable_value 存储每个 variable_code 的值。例如对于 AOP 605 代码会有 5.
的值
我的麻烦来了:我需要为属于某个 cpv_id 组(来自用户请求)的所有公司的每个 variable_code 求和 variable_value。我只设法创建了一个查询,该查询将对一个代码的值求和。可以看到在SQLFiddle中提供:http://sqlfiddle.com/#!2/a3e296/2
在输出中您会看到我正在对代码 AOP 201 执行 totalSum,并且在我的应用程序中我正在执行另一个查询来计算 的总和AOP 605,但我认为这并不理想,可能有一些方法可以获得 AOP 201 和 AOP 605[=90 的 SUM =] 在同一个查询中。然后我可以调用它们 sum201 和 sum605 并将它们都显示在我的应用程序中。有人可以告诉我这是否可能吗?你能告诉我将收集 AOP 201 和 AOP 605 的总和并将它们存储在单独的值中以便我可以显示 [= 的总和的查询吗53=]AOP 201 和 AOP 605 的总和 ???
还有一个棘手的部分。正如您在我的查询中看到的那样,我遇到了这种情况:AND company_report.report_year = 2013
。这并不理想,因为有些公司不会在 2013 年制作报告,有些公司只有 2012 年的报告。我将不得不接受他们有的最后一年的报告。因此,例如,如果某些公司在 2012 年和 2013 年有报告,我将只计算 2013 年。同样,如果其他公司有2009年和2012年的报告,我就只算2012年。
你能帮我解决这个问题吗?
我已经为此苦苦挣扎了 3 天,但找不到解决方案。谢谢你的时间。
编辑:我需要 AOP 605 和 AOP 201 的总和作为单独的列,因此我可以在我的应用程序代码中访问它们,例如$sumOne - 显示 AOP 605 和 $sumTwo - 显示 AOP 201.
的总和
更多信息:我正在尝试将这两个查询合并为一个:
SELECT DISTINCT company_report.*, floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
AND club_offer_cpv.cpv_id LIKE '66%'
AND company_report.variable_code = 'AOP 201'
AND company_report.report_year = 2013
和
SELECT DISTINCT company_report.*, floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
AND club_offer_cpv.cpv_id LIKE '66%'
AND company_report.variable_code = 'AOP 605'
AND company_report.report_year = 2013
请注意每个查询中的这些行:
company_report.variable_code = 'AOP 605'
company_report.variable_code = 'AOP 201'
所以在一个查询中,我想为具有 variable_code = "AOP 605" 的行获取 variable_value 的总和,在第二个查询中我需要获取 variable_value 的总和具有 variable_code = "AOP 201" 的行。但我在想,一定有某种方法可以在一个查询中获得这两个总和,但我可以通过这种方式分别显示它们,比如:
AOP 605 的总和是:123
AOP 201 的总和是:345
您似乎要查找的是 SELECT 的 GROUP BY 部分,如下所示:
SELECT DISTINCT company_report.*, floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
AND club_offer_cpv.cpv_id LIKE '66%'
AND company_report.variable_code IN ('AOP 201','AOP 605')
AND company_report.report_year = 2013
GROUP BY company_report.variable_code
是吗?
我有 6 个 table 彼此相关。我有一个收集所有数据的 JOIN 查询。我首先提供 cpv_id 作为来自用户的变量,然后最后我需要获取与该 cpv_id 相关的每个公司的报告(通过许多 table 之间他们 ).
基本上,流程是这样的:
我从用户那里得到 cpv_id,我在 club_offer_cpv table 中找到它并且我得到 club_offer_id,所以我可以连接到 club_offer table。然后在 club_offer table 我得到 club_id 所以我可以连接到 club table。然后在 club table 我得到 users_id 所以我可以连接到 users table。然后在 users table 我得到 company_id 所以我可以连接到 company table。在 company table 我得到 special_id 所以我可以连接到最后一个 table company_reports。这是我需要从中获取数据的地方。我需要这个:
这里我有3个兴趣领域:report_year、variable_code和 variable_value.
report_year 存储公司报告的年份。
variable_code 存储所做报告的类型。在我关于 SQL Fiddle 的简化示例中,可能的值是 AOP 605 和 AOP 201.
variable_value 存储每个 variable_code 的值。例如对于 AOP 605 代码会有 5.
我的麻烦来了:我需要为属于某个 cpv_id 组(来自用户请求)的所有公司的每个 variable_code 求和 variable_value。我只设法创建了一个查询,该查询将对一个代码的值求和。可以看到在SQLFiddle中提供:http://sqlfiddle.com/#!2/a3e296/2
在输出中您会看到我正在对代码 AOP 201 执行 totalSum,并且在我的应用程序中我正在执行另一个查询来计算 的总和AOP 605,但我认为这并不理想,可能有一些方法可以获得 AOP 201 和 AOP 605[=90 的 SUM =] 在同一个查询中。然后我可以调用它们 sum201 和 sum605 并将它们都显示在我的应用程序中。有人可以告诉我这是否可能吗?你能告诉我将收集 AOP 201 和 AOP 605 的总和并将它们存储在单独的值中以便我可以显示 [= 的总和的查询吗53=]AOP 201 和 AOP 605 的总和 ???
还有一个棘手的部分。正如您在我的查询中看到的那样,我遇到了这种情况:AND company_report.report_year = 2013
。这并不理想,因为有些公司不会在 2013 年制作报告,有些公司只有 2012 年的报告。我将不得不接受他们有的最后一年的报告。因此,例如,如果某些公司在 2012 年和 2013 年有报告,我将只计算 2013 年。同样,如果其他公司有2009年和2012年的报告,我就只算2012年。
你能帮我解决这个问题吗?
我已经为此苦苦挣扎了 3 天,但找不到解决方案。谢谢你的时间。
编辑:我需要 AOP 605 和 AOP 201 的总和作为单独的列,因此我可以在我的应用程序代码中访问它们,例如$sumOne - 显示 AOP 605 和 $sumTwo - 显示 AOP 201.
的总和更多信息:我正在尝试将这两个查询合并为一个:
SELECT DISTINCT company_report.*, floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
AND club_offer_cpv.cpv_id LIKE '66%'
AND company_report.variable_code = 'AOP 201'
AND company_report.report_year = 2013
和
SELECT DISTINCT company_report.*, floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
AND club_offer_cpv.cpv_id LIKE '66%'
AND company_report.variable_code = 'AOP 605'
AND company_report.report_year = 2013
请注意每个查询中的这些行:
company_report.variable_code = 'AOP 605'
company_report.variable_code = 'AOP 201'
所以在一个查询中,我想为具有 variable_code = "AOP 605" 的行获取 variable_value 的总和,在第二个查询中我需要获取 variable_value 的总和具有 variable_code = "AOP 201" 的行。但我在想,一定有某种方法可以在一个查询中获得这两个总和,但我可以通过这种方式分别显示它们,比如:
AOP 605 的总和是:123
AOP 201 的总和是:345
您似乎要查找的是 SELECT 的 GROUP BY 部分,如下所示:
SELECT DISTINCT company_report.*, floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.special_id = company.special_id
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
AND club_offer_cpv.cpv_id LIKE '66%'
AND company_report.variable_code IN ('AOP 201','AOP 605')
AND company_report.report_year = 2013
GROUP BY company_report.variable_code
是吗?