SELECT ROUND(count(event_date)/count(First_Login.player_id), 2) AS fraction FROM ( SELECT player_id, min(event_date) AS first_login FROM Activity GROUPBY player_id) First_Login LEFTJOIN Activity ON First_Login.player_id = Activity.player_id AND ADDDATE(First_Login.first_login, INTERVAL1DAY) = Activity.event_date
SELECT T.Department, E.name AS Employee, T.Salary FROM Employee E INNERJOIN ( SELECT D.id as DID, D.name AS Department, E.name AS Employee, MAX(E.salary) AS Salary FROM Employee E JOIN Department D ON E.departmentId = D.id GROUPBY E.departmentId ) T ON T.Salary = E.salary AND T.DID = E.departmentId;
思路2
子查询,通过group by找出每个部门最大salary
员工表啊与部门表合并,若(员工薪水, 部门id)存在于子查询中,则筛选出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SELECT Department.name AS'Department', Employee.name AS'Employee', Salary FROM Employee JOIN Department ON Employee.DepartmentId = Department.Id WHERE (Employee.DepartmentId , Salary) IN ( SELECT DepartmentId, MAX(Salary) FROM Employee GROUPBY DepartmentId )
CREATEFUNCTION getNthHighestSalary(N INT) RETURNSINT BEGIN DECLARE M INT; SET M = N-1; RETURN ( SELECT IFNULL((SELECTDISTINCT salary FROM Employee ORDERBY salary DESC LIMIT 1OFFSET M), NULL) ); END
SELECT D.name AS Department, E.name AS Employee, E.salary AS Salary FROM Employee E, Department AS D, Employee E2 WHERE D.id = E.departmentId AND E2.departmentId = E.departmentId AND E.salary <= E2.salary groupby D.ID,E.Name havingcount(distinct E2.Salary) <=3 orderby D.Name, E.Salary desc
select A from tableA A1 where col1 in ( select col1 from tableA A2 where A1.xxx = A2.xxx ) and col2 in ( select col2 from tableA A2 where A1.xxx = A2.xxx ) and col3 in ( select col2 from tableA A2 where A1.xxx = A2.xxx ) -- 可以优化为 select A from tableA A1 where col1 || col2 || col3 in ( select col1 || col2 || col3 from tableA A2 ) -- or select A from tableA A1 where (col1, col2, col3) in ( select col1, col2, col3 from tableA A2 )