My LeetCode Log
1.0.0
1.0.0
  • Preface
  • Algorithms
  • Database
    • 175.(Easy)组合两个表
    • 176.(Easy)第二高的薪水
    • 177.(Medium)第N高的薪水
    • 178.(Medium)分数排名
    • 180.(Medium)连续出现的数字
    • 181.(Easy)超过经理收入的员工
    • 182.(Easy)查找重复的电子邮箱
    • 183.(Easy)从不订购的客户
    • 184.(Medium)部门工资最高的员工
    • 185.(Hard)部门工资前三高的所有员工
    • 196.(Easy)删除重复的电子邮箱
    • 197.(Easy)上升的温度
    • 262.(Hard)行程和用户
    • 511.(Easy)游戏玩法分析 I
    • 512.(Easy)游戏玩法分析 II
    • 534.(Medium)游戏玩法分析 III
    • 550.(Medium)游戏玩法分析 IV
    • 569.(Hard)员工薪水中位数
    • 570.(Medium)至少有5名直接下属的经理
    • 571.(Hard)给定数字的频率查询中位数
    • 574.(Medium)当选者
    • 577.(Easy)员工奖金
    • 578.(Medium)查询回答率最高的问题
    • 579.(Hard)查询员工的累计薪水
    • 580.(Medium)统计各专业学生人数
    • 584.(Easy)寻找用户推荐人
    • 585.(Medium)2016年的投资
    • 586.(Easy)订单最多的客户
    • 595.(Easy)大的国家
    • 596.(Easy)超过5名学生的课
    • 597.(Easy)好友申请 I:总体通过率
    • 601.(Hard)体育馆的人流量
    • 602.(Medium)好友申请 II :谁有最多的好友
    • 603.(Easy)连续空余座位
    • 607.(Easy)销售员
    • 608.(Medium)树节点
    • 610.(Easy)判断三角形
    • 612.(Medium)平面上的最近距离
    • 613.(Easy)直线上的最近距离
    • 614.(Medium)二级关注者
    • 615.(Hard)平均工资:部门与公司比较
    • 618.(Hard)学生地理信息报告
    • 619.(Easy)只出现一次的最大数字
    • 620.(Easy)有趣的电影
    • 626.(Medium)换座位
    • 627.(Easy)变更性别
    • 1045.(Medium)买下所有产品的客户
    • 1050.(Easy)合作过至少三次的演员和导演
    • 1068.(Easy)产品销售分析 I
    • 1069.(Easy)产品销售分析 II
    • 1070.(Medium)产品销售分析 III
    • 1075.(Easy)项目员工 I
    • 1076.(Easy)项目员工II
    • 1077.(Medium)项目员工 III
    • 1082.(Easy)销售分析 I
    • 1083.(Easy)销售分析 II
    • 1084.(Easy)销售分析III
    • 1097.(Hard)游戏玩法分析 V
    • 1098.(Medium)小众书籍
    • 1107.(Medium)每日新用户统计
    • 1112.(Medium)每位学生的最高成绩
    • 1113.(Easy)报告的记录
    • 1126.(Medium)查询活跃业务
    • 1127.(Hard)用户购买平台
    • 1132.(Medium)报告的记录 II
    • 1141.(Easy)查询近30天活跃用户数
    • 1142.(Easy)过去30天的用户活动 II
    • 1148.(Easy)文章浏览 I
    • 1149.(Medium)文章浏览 II
    • 1158.(Medium)市场分析 I
    • 1159.(Hard)市场分析 II
    • 1164.(Medium)指定日期的产品价格
    • 1173.(Easy)即时食物配送 I
    • 1174.(Medium)即时食物配送 II
    • 1179.(Easy)重新格式化部门表
    • 1193.(Medium)每月交易 I
    • 1194.(Hard)锦标赛优胜者
    • 1204.(Medium)最后一个能进入电梯的人
    • 1205.(Medium)每月交易II
    • 1211.(Easy)查询结果的质量和占比
    • 1212.(Medium)查询球队积分
    • 1225.(Hard)报告系统状态的连续日期
    • 1241.(Easy)每个帖子的评论数
    • 1251.(Easy)平均售价
    • 1264.(Medium)页面推荐
    • 1270.(Medium)向公司CEO汇报工作的所有人
    • 1280.(Easy)学生们参加各科测试的次数
    • 1285.(Medium)找到连续区间的开始和结束数字
    • 1294.(Easy)不同国家的天气类型
    • 1303.(Easy)求团队人数
    • 1308.(Medium)不同性别每日分数总计
    • 1321.(Medium)餐馆营业额变化增长
    • 1322.(Easy)广告效果
    • 1327.(Easy)列出指定时间段内所有的下单产品
    • 1336.(Hard)每次访问的交易次数
    • 1341.(Medium)电影评分
    • 1350.(Easy)院系无效的学生
    • 1355.(Medium)活动参与者
    • 1364.(Medium)顾客的可信联系人数量
    • 1369.(Hard)获取最近第二次的活动
    • 1378.(Easy)使用唯一标识码替换员工ID
    • 1384.(Hard)按年度列出销售总额
    • 1393.(Medium)股票的资本损益
    • 1398.(Medium)购买了产品 A 和产品 B 却没有购买产品 C 的顾客
    • 1407.(Easy)排名靠前的旅行者
    • 1412.(Hard)查找成绩处于中游的学生
    • 1421.(Medium)净现值查询
    • 1435.(Easy)制作会话柱状图
    • 1440.(Medium)计算布尔表达式的值
    • 1445.(Medium)苹果和桔子
    • 1454.(Medium)活跃用户
    • 1459.(Medium)矩形面积
    • 1468.(Medium)计算税后工资
    • 1479.(Hard)周内每天的销售情况
    • 1485.(Easy)按日期分组销售产品
    • 1495.(Easy)上月播放的儿童适宜电影
    • 1501.(Medium)可以放心投资的国家
    • 1511.(Easy)消费者下单频率
    • 1517.(Easy)查找拥有有效邮箱的用户
    • 1527.(Easy)患某种疾病的患者
    • 1532.(Medium)最近的三笔订单
    • 1543.(Easy)产品名称格式修复
    • 1549.(Medium)每件商品的最新订单
    • 1555.(Medium)银行账户概要
    • 1565.(Easy)按月统计订单数与顾客数
    • 1571.(Easy)仓库经理
    • 1581.(Easy)进店却未进行过交易的顾客
    • 1587.(Easy)银行账户概要 II
    • 1596.(Medium)每位顾客最经常订购的商品
    • 1607.(Easy)没有卖出的卖家
    • 1613.(Medium)找到遗失的ID
    • 1623.(Easy)三人国家代表队
    • 1633.(Easy)各赛事的用户注册率
    • 1635.(Hard)Hopper 公司查询 I
    • 1645.(Hard)Hopper Company Queries II
    • 1651.(Hard)Hopper Company Queries III
    • 1661.(Easy)每台机器的进程平均运行时间
    • 1667.(Easy)修复表中的名字
    • 1677.(Easy)发票中的产品金额
    • 1683.(Easy)无效的推文
    • 1693.(Easy)每天的领导和合伙人
    • 1699.(Medium)两人之间的通话次数
    • 1709.(Medium)访问日期之间最大的空档期
    • 1715.(Medium)苹果和橘子的个数
    • 1729.(Easy)求关注者的数量
    • 1731.(Easy)每位经理的下属员工数量
    • 1741.(Easy)查找每个员工花费的总时间
    • 1747.(Medium)应该被禁止的Leetflex账户
    • 1757.(Easy)可回收且低脂的产品
    • 1767.(Hard)Find the Subtasks That Did Not Execute
    • 1777.(Easy)每家商店的产品价格
    • 1783.(Medium)Grand Slam Titles
    • 1789.(Easy)Primary Department for Each Employee
    • 1795.(Easy)Rearrange Products Table
    • 1809.(Easy)Ad-Free Sessions
    • 1811.(Medium)Find Interview Candidates
    • 1821.(Easy)Find Customers With Positive Revenue this Year
    • 1831.(Medium)Maximum Transaction Each Day
    • 1841.(Medium)League Statistics
    • 1843.(Medium)Suspicious Bank Accounts
    • 1853.(Easy)Convert Date Format
    • 1867.(Medium)Orders With Maximum Quantity Above Average
    • 1873.(Easy)Calculate Special Bonus
    • 1875.(Medium)Group Employees of the Same Salary
    • 1890.(Easy)The Latest Login in 2020
    • 1892.(Hard)Page Recommendations II
    • 1907.(Medium)Count Salary Categories
    • 1917.(Hard)Leetcodify Friends Recommendations
    • 1919.(Hard)Leetcodify Similar Friends
    • 1934.(Medium)Confirmation Rate
    • 1939.(Easy)Users That Actively Request Confirmation Messages
  • 剑指offer
    • 连续子数组的最大和
    • 构建乘积数组
    • 跳台阶n
    • 跳台阶2
    • 求1至n的和
    • 2x1矩形覆盖
    • 二进制数中1的个数
    • 十进制数中1的个数
    • 二分查找
Powered by GitBook
On this page
  • Solution
  • Schema

Was this helpful?

  1. Database

1875.(Medium)Group Employees of the Same Salary

Table: Employees

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
| salary      | int     |
+-------------+---------+
employee_id is the primary key for this table.
Each row of this table indicates the employee ID, employee name, and salary.

A company wants to divide the employees into teams such that all the members on each team have the same salary. The teams should follow these criteria:

  • Each team should consist of at least two employees.

  • All the employees on a team should have the same salary.

  • All the employees of the same salary should be assigned to the same team.

  • If the salary of an employee is unique, we do not assign this employee to any team.

  • A team's ID is assigned based on the rank of the team's salary relative to the other teams' salaries, where the team with the lowest salary has team_id = 1. Note that the salaries for employees not on a team are not included in this ranking.

Write an SQL query to get the team_id of each employee that is in a team.

Return the result table ordered by team_id in ascending order. In case of a tie, order it by employee_id in ascending order.

The query result format is in the following example:

Employees table:
+-------------+---------+--------+
| employee_id | name    | salary |
+-------------+---------+--------+
| 2           | Meir    | 3000   |
| 3           | Michael | 3000   |
| 7           | Addilyn | 7400   |
| 8           | Juan    | 6100   |
| 9           | Kannon  | 7400   |
+-------------+---------+--------+

Result table:
+-------------+---------+--------+---------+
| employee_id | name    | salary | team_id |
+-------------+---------+--------+---------+
| 2           | Meir    | 3000   | 1       |
| 3           | Michael | 3000   | 1       |
| 7           | Addilyn | 7400   | 2       |
| 9           | Kannon  | 7400   | 2       |
+-------------+---------+--------+---------+

Meir (employee_id=2) and Michael (employee_id=3) are in the same team because they have the same salary of 3000.
Addilyn (employee_id=7) and Kannon (employee_id=9) are in the same team because they have the same salary of 7400.
Juan (employee_id=8) is not included in any team because their salary of 6100 is unique (i.e. no other employee has the same salary).
The team IDs are assigned as follows (based on salary ranking, lowest first):

- team_id=1: Meir and Michael, salary of 3000

- team_id=2: Addilyn and Kannon, salary of 7400

Juan's salary of 6100 is not included in the ranking because they are not on a team.

来源:力扣(LeetCode)

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Solution

with t as (
    SELECT salary, ROW_NUMBER() OVER(ORDER BY salary) team_id 
    FROM employees
    GROUP BY salary
    HAVING COUNT(1) >1 
)
SELECT e.*, t.team_id
FROM t LEFT JOIN Employees e ON t.salary = e.salary
ORDER BY team_id, employee_id;

Schema

Create table If Not Exists Employees (employee_id int, name varchar(30), salary int);
Truncate table Employees;
insert into Employees (employee_id, name, salary) values ('2', 'Meir', '3000');
insert into Employees (employee_id, name, salary) values ('3', 'Michael', '3000');
insert into Employees (employee_id, name, salary) values ('7', 'Addilyn', '7400');
insert into Employees (employee_id, name, salary) values ('8', 'Juan', '6100');
insert into Employees (employee_id, name, salary) values ('9', 'Kannon', '7400');
Previous1873.(Easy)Calculate Special BonusNext1890.(Easy)The Latest Login in 2020

Last updated 3 years ago

Was this helpful?

链接:

https://leetcode-cn.com/problems/group-employees-of-the-same-salary