Answer the question
In order to leave comments, you need to log in
How to write a query with proportions to display statistics in MySQL?
Greetings! I can't figure out how to write a SQL query. There are initial data:
And you need to calculate the cost for each operator for each city, depending on how many applications from the total mass the operator has processed.
I calculate the cost per operator in each city as follows:
The number of operator applications in the city / the total number of applications in the city * the expense in this city
Answer the question
In order to leave comments, you need to log in
Something like this:
select
t3.user_id,
t4.city_id,
(t3.oper_zayavki / t4.`Заявки`) * t4.`Расход`
from Goroda_Statistika t4
inner join
(
select
t1.user_id,
t2.city_id,
sum(t2.`Заявки`) as oper_zayavki
from Operatory t1
join Operatory_Statistika t2 on t2.user_id = t1.user_id
group by t1.user_id, t2.city_id
) t3 on t3.city_id = t4.city_id
join three tables, group by user_id, city_id and correctly compose the result from aggregating functions
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question