K
K
Kirill Petrov2018-07-25 17:17:37
MySQL
Kirill Petrov, 2018-07-25 17:17:37

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:
5b58852fb7079331144977.png
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.
5b5885932bf6c684507556.png
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

2 answer(s)
N
nozzy, 2018-07-26
@Recosh

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

L
Lazy @BojackHorseman MySQL, 2018-07-25
Tag

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 question

Ask a Question

731 491 924 answers to any question