[英]Inner join query with “on” if condition
How to write a query to get the below output (see table no. 4)如何编写查询以获得以下 output(见表 4)
table user : role - 1:manager, 2:employee
+----+------+-----------+--------+
| id | name | role | salary |
+----+------+-----------+--------+
| 1 | a | 1 | 0 |
| 2 | b | 1 | 0 |
| 3 | c | 2 | 10 |
| 4 | d | 2 | 20 |
| 5 | e | 2 | 30 |
| 6 | f | 2 | 40 |
+----+------+-----------+--------+
table city
+----+--------+------+
| id | name | type |
+----+--------+------+
| 1 | cityA | 1 |
| 2 | cityB | 2 |
+----+--------+------+
table user_city_mapping
+----+-----------+---------+
| id | user_id | city_id |
+----+-----------+---------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 4 | 1 |
| 6 | 5 | 2 |
| 7 | 6 | 2 |
+----+-----------+---------+
output required
+------+-------+
| name | total |
+------+-------+
| a | 30 |
| b | 100 |
| c | 10 |
| d | 20 |
| e | 70 |
| f | 70 |
+------+-------+
user "b", "e", "f" belong to "cityB" which is of type 2.用户“b”、“e”、“f”属于类型 2 的“cityB”。
user "c" and "d" falls under manager "a"用户“c”和“d”属于经理“a”
An explanation for the output required:需要对 output 的解释:
In short,
if an employee falls under any manager, the manager get the sum of the salary of the employee under him.
if the employee belongs to "type 1" city he gets his salary.
if an employee belongs to the "type 2" city he gets the sum of all employees belongs to that city.
The above-provided details are the requirement and output.以上提供的详细信息是要求和 output。 I am not able to get the query for the desired output.
我无法获得所需 output 的查询。
Something which i tried我试过的东西
SELECT b.user_id, sum(salary)
FROM user_city_mapping a
INNER JOIN user_city_mapping b
ON a.city_id = b.city_id
INNER JOIN user
ON a.user_id = user.id AND role = 2
GROUP BY b.user_id
Quite messy and involves parsing the data multiple times.相当混乱,涉及多次解析数据。 Simplest is to work out the salary by user using a correlated sub query based on city type the total salary for type 2 can be calculated.
最简单的方法是使用基于城市类型的相关子查询按用户计算工资,可以计算类型 2 的总工资。 Working out the salary by city and who manages that city is achieved in the sub query x by first working out a city total and then aggregating by manager The outer query then selects distinct to get rid of duplicate manager rows.
在子查询 x 中按城市计算工资和管理该城市的人是通过首先计算城市总数然后按经理聚合来实现的。外部查询然后选择 distinct 以消除重复的经理行。
select distinct uname,
case when salarybyuser = 0 then salarybycity
else salarybyuser
end salary
from
(
select u.id uid,u.name uname,ucm.*,c.id cid,
case when c.type = 1 then salary
when c.type = 2 and role <> 1 then
(select sum(salary)
from user u
join user_city_mapping ucm on ucm.user_id = u.id
join city c on c.id = ucm.city_id
where c.type = 2 and role <> 1
)
else 0
end as salarybyuser,
x.salarybycity
from user u
join user_city_mapping ucm on ucm.user_id = u.id
join city c on c.id = ucm.city_id
left join
(
select ucm.user_id uid,u.name, sum(SalaryByCity) salarybycity
from user_city_mapping ucm
join user u on u.id = ucm.user_id
join
(
select c1.id cid,sum(salary) SalaryByCity
from user u1
join user_city_mapping ucm1 on ucm1.user_id = u1.id
join city c1 on c1.id = ucm1.city_id
group by c1.id
) s on s.cid = ucm.city_id
where u.role = 1
group by u.id,u.name
) x on x.uid = u.id
order by c.id,u.role,u.id
) y
order by uid
;
+-------+--------+
| uname | salary |
+-------+--------+
| a | 30 |
| b | 100 |
| c | 10 |
| d | 20 |
| e | 70 |
| f | 70 |
+-------+--------+
6 rows in set (0.002 sec)
Seems messy but this is really 3 queries compressed into 1.看起来很乱,但这实际上是将 3 个查询压缩为 1 个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.