[英]How to do two MySQL SUMs based on different criterias
I have 6 tables related to each other. 我有6个彼此相关的表。 I have a JOIN query gathering all data.
我有一个收集所有数据的JOIN查询。 I am starting by providing cpv_id as a variable coming from users, and then at the end I need to get reports for each company that is related to that cpv_id ( through many tables between them ).
我首先提供cpv_id作为来自用户的变量,然后最后我需要获取与该cpv_id相关的每个公司的报告(通过它们之间的许多表)。
Basically, this is the flow: 基本上,这是流程:
I get cpv_id from users, I find it in club_offer_cpv table and I get club_offer_id so I can connect to club_offer table. 我从用户那里获得了cpv_id ,我在club_offer_cpv表中找到了它,并且得到了club_offer_id,因此可以连接到club_offer表。 Then in club_offer table I get club_id so I can connect to club table.
然后在club_offer表中获得club_id,以便我可以连接到club表。 Then in club table I get users_id so I can connect to users table.
然后在club表中获取users_id,以便可以连接到users表。 Then in users table I get company_id so I can connect to company table.
然后在用户表中,我得到company_id,以便我可以连接到公司表。 In company table I get special_id so I can connect to the last table company_reports .
在company表中,我得到了special_id,因此我可以连接到最后一个表company_reports 。 This is where I need to get my data from.
这是我需要从中获取数据的地方。 And I need this:
我需要这个:
Here I have 3 field of interest: report_year , variable_code and variable_value . 在这里,我有3个感兴趣的领域: report_year , variable_code和variable_value 。
report_year stores the year when company report was made. report_year存储制作公司报告的年份。
variable_code stores the type of report that was made. variable_code存储所生成报告的类型。 In my simplified example on SQL Fiddle, possible values are AOP 605 and AOP 201 .
在我关于SQL Fiddle的简化示例中,可能的值为AOP 605和AOP 201 。
variable_value stores the value for each variable_code. variable_value存储每个variable_code的值。 For example for AOP 605 code there will be value of 5 .
例如,对于AOP 605代码,值为5 。
Here comes my trouble: I need to sum variable_value for each variable_code for all companies that belong to some cpv_id group ( came from user request ). 我的麻烦来了:我需要对属于某个cpv_id组(来自用户请求)的所有公司的每个variable_code的variable_value求和。 I only managed to create query that will sum values for one code.
我只设法创建将对一个代码的值求和的查询。 You can see that in SQL Fiddle provided: http://sqlfiddle.com/#!2/a3e296/2
您可以在提供的SQL Fiddle中看到: http ://sqlfiddle.com/#!2/a3e296/2
In the output you will see that I am doing totalSum for code AOP 201 , and In my application I am executing one more query to calculate sum for AOP 605 , but I believe that is not ideal, and probably there is some way to get SUMs for both AOP 201 and AOP 605 in the same query. 在输出中,您将看到我正在对代码AOP 201进行totalSum,并且在我的应用程序中,我将再执行一个查询以计算AOP 605的总和,但是我认为这并不理想,并且可能有一些获取SUM的方法对于同一查询中的AOP 201和AOP 605 。 I can call them then sum201 and sum605 and display them both in my application.
我可以先调用它们sum201和sum605,然后在我的应用程序中同时显示它们。 Can someone please tell me if this is possible ?
有人可以告诉我这是否可行吗? Can you show me the query that will collect sums for AOP 201 and AOP 605 and store them in separate values so I can display sum for AOP 201 and sum for AOP 605 ???
您能告诉我查询将收集AOP 201和AOP 605的总和并将它们存储在单独的值中,以便我可以显示AOP 201的总和和AOP 605的总和吗 ?
Also there is one more tricky part. 另外还有一个棘手的部分。 As you can see in my query I am having this condition:
AND company_report.report_year = 2013
. 如您在我的查询中所看到的,我遇到这种情况:
AND company_report.report_year = 2013
。 This is not ideal, because some companies will not have reports made in 2013, some of them have only for 2012. I would have to take whatever is the last year of report that they have. 这是不理想的,因为有些公司不会在2013年提交报告,而有些公司只提供2012年的报告。我必须拿走他们所拥有报告的最后一年。 So for example if some company have reports in 2012 and 2013, I will take only 2013 in count.
因此,举例来说,如果某家公司有2012年和2013年的报告,那么我将只考虑2013年。 Similarly, if some other company have reports for 2009 and 2012, I will take only 2012 in count.
同样,如果其他公司有2009年和2012年的报告,则我只计算2012年。
Can you help me with this also ? 您还能帮我吗?
I am struggling with this for 3 day now, and can not find solution. 我为此苦苦挣扎了3天,找不到解决方法。 Thank you for your time.
感谢您的时间。
EDIT: I need sums of AOP 605 and AOP 201 as seprate columns, so I can access them in my application code like $sumOne - displays the sum for AOP 605 and $sumTwo - displays the sum for AOP 201 . 编辑:我需要AOP 605和AOP 201的总和作为单独的列,因此我可以在我的应用程序代码中访问它们,例如$ sumOne-显示AOP 605的总和和$ sumTwo-显示AOP 201的总和。
MORE INFO : I am trying to merge these two queries into one: 更多信息 :我试图将这两个查询合并为一个:
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
AND 和
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
Please pay attention on these lines in each query: 请在每个查询中注意以下几行:
company_report.variable_code = 'AOP 605'
company_report.variable_code = 'AOP 201'
So in one query I want to get sum of variable_value for rows having variable_code = "AOP 605", and in second I need to get sum of variable_value for rows having variable_code = "AOP 201". 因此,在一个查询中,我想为variable_code =“ AOP 605”的行获取variable_value的总和,在第二个查询中,我需要为variable_code =“ AOP 201”的行获取variable_value的总和。 But I am thinking that there must be some way to get both sums in one query, but on the way that I can display them bot separately, like:
但是我认为必须有某种方法可以在一个查询中获得两个总和,但是在可以分别显示它们的方式上,例如:
Sum of AOP 605 is : 123 AOP 605的总和是:123
Sum of AOP 201 is : 345 AOP 201的总和是:345
What you seem to be looking for is the GROUP BY part of a SELECT, like this: 您似乎正在寻找的是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
Is that it? 是吗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.