求按照城市分布的平均薪资和总薪资并按照城市内的总薪资倒叙排序只取每个城市总薪资的top1,城市和薪资分别为两个表,hive语法
时间: 2023-07-16 07:12:14 浏览: 85
假设城市表名为 city,包含字段为 city_name,薪资表名为 salary,包含字段为 city_name 和 salary。
按照城市分布的平均薪资和总薪资:
```sql
SELECT
city_name,
AVG(salary) AS avg_salary,
SUM(salary) AS total_salary
FROM
salary
GROUP BY
city_name;
```
按照城市内的总薪资倒序排序,只取每个城市总薪资的 top1:
```sql
SELECT
city_name,
SUM(salary) AS total_salary
FROM
salary
GROUP BY
city_name
ORDER BY
total_salary DESC
LIMIT
1;
```
将两个 SQL 语句结合起来,得到如下的 SQL 语句:
```sql
SELECT
c.city_name,
AVG(s.salary) AS avg_salary,
SUM(s.salary) AS total_salary
FROM
salary s
JOIN
city c
ON
s.city_name = c.city_name
GROUP BY
c.city_name
ORDER BY
SUM(s.salary) DESC
LIMIT
1;
```
这个 SQL 语句的含义是:先将城市表和薪资表连接起来,然后按照城市分组,计算每个城市的平均薪资和总薪资,最后按照总薪资倒序排序,只取排序后的第一个结果。
阅读全文