How to do two MySQL SUMs based on different criter

2019-06-04 04:17发布

I have 6 tables related to each other. I have a JOIN query gathering all data. 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 ).

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. Then in club_offer table I get club_id so I can connect to club table. Then in club table I get users_id so I can connect to users table. Then in users table I get company_id so I can connect to company table. In company table I get special_id so I can connect to the last table 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.

report_year stores the year when company report was made.
variable_code stores the type of report that was made. In my simplified example on SQL Fiddle, possible values are AOP 605 and AOP 201.
variable_value stores the value for each variable_code. For example for AOP 605 code there will be value of 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 ). 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

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. I can call them then sum201 and sum605 and display them both in my application. 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 ???

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. 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. So for example if some company have reports in 2012 and 2013, I will take only 2013 in count. Similarly, if some other company have reports for 2009 and 2012, I will take only 2012 in count.

Can you help me with this also ?

I am struggling with this for 3 day now, and can not find solution. 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.

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". 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
Sum of AOP 201 is : 345

1条回答
放荡不羁爱自由
2楼-- · 2019-06-04 04:35

What you seem to be looking for is the GROUP BY part of a SELECT, like this:

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?

查看更多
登录 后发表回答