|
LeetcodeDatabase-我的汇总(二)
- 第一名 99
- 第二名 98
- 第二名 98
- 第四名 97
|
- 第一名 99
- 第二名 98
- 第二名 98
- 第三名 97
|
|
- SELECT d.Name Department, temp.Name Employee, temp.Salary Salary
- FROM Department d,
- ( SELECT em.Id Id,em.Name Name,em.Salary Salary,em.DepartmentId dep
- FROM Employee em
- WHERE (
- #count records whose salary large than outer salary
- SELECT count(1) FROM Employee emp
- WHERE em.DepartmentId=emp.DepartmentId
- And em.Salary
- )<=2) temp
- #for record with highest salary, 0 record large than it; for record with second highest #salary, 1 record large than it. To get top three, records large than target record should #<=2
- WHERE temp.dep=d.Id
- ORDER BY Department ,Salary DESC;
- #in this way, as ‘count’ does not remove duplicate records, there will be a gap after #records with same salary
|
- SELECT d.Name Department, temp.Name Employee, temp.Salary Salary
- FROM Department d,
- ( SELECT em.Id Id,em.Name Name,em.Salary Salary,em.DepartmentId dep
- FROM Employee em
- WHERE (
- #with ‘count distinct’, the result is same as with no duplicate records
- SELECT count(distinct emp.Salary) FROM Employee emp
- WHERE em.DepartmentId=emp.DepartmentId
- And em.Salary<=emp.Salary
- )<=3) temp
- WHERE temp.dep=d.Id
- ORDER BY Department ,Salary DESC;
|
- select a.depart, a.name, a.salary
- from (
- select d.name depart, e.name name, rank()over(partition by d.id order by e.salary desc) rank, e.salary salary
- from department d, employee e
- where d.id=e.departmentid
- ) a
- where a.rank<=3;
|
-
create table Logs(id int,num int);
-
insert into Logs(id,num) values(1,1);
-
insert into Logs(id,num) values(2,1);
-
insert into Logs(id,num) values(3,1);
-
insert into Logs(id,num) values(4,2);
-
insert into Logs(id,num) values(5,1);
-
insert into Logs(id,num) values(6,2);
-
insert into Logs(id,num) values(7,2);
|
- Select distinct la.num
- From Logs la ,Logs lb,Logs lc
- where la.id+1=lb.id and lb.id+1=lc.id and la.num=lb.num and lb.num=lc.num;
|
- Select distinct la.num
- From Logs la
- Where(
- #if all the three records have same num value, the ‘count’get result 1
- Select count(distinct lb.num)
- From Logs lb
- Where la.id<=lb.id and lb.id<=la.id+2
- #filter records with id between la.id and la.id+2, get no more than 3 records
- )=1;
|
- Select distinct la.num
- From Logs la
- Where(
- #if all the three records have same num value, the ‘count’get result 1
- Select count(distinct lb.num)
- From Logs lb
- Where la.id<=lb.id and lb.id<=la.id+2
- #filter records with id between la.id and la.id+2, get no more than 3 records
- )=1
- and
- # make sure 3 rows selected
- ( Select count(1)
- From Logs lb
- Where la.id<=lb.id and lb.id<=la.id+2 )=3;
|
- SELECT dep.name depart, ttm.na employee, ttm.sal salary
- FROM Department dep,(
- SELECT emp.Name na, emp.salary sal, emp.departmentid dd
- FROM Employee emp,(
- SELECT departmentid did, MAX(salary) ms
- FROM Employee
- GROUP BY departmentid) temp
- WHERE emp.salary=temp.ms
- AND temp.did=emp.departmentid) ttm
- WHERE dep.id=ttm.dd;
|
- select dep.name department, max(em.salary) salary
- from Employee em, Department dep
- where em.departmentid=dep.id
- group by dep.name ;
|
- ?
|
- select eb.dname department, ea.name employee, ea.salary salary
- from employee ea, (
- select max(salary) ms, dep.name dname
- from Employee em, Department dep
- where em.departmentid=dep.id
- group by em.departmentid ) eb
- where ea.salary=eb.ms;
|
- ?
|
-
DROP TABLE Employee PUGRE;
-
CREATE TABLE Employee(id INT, salary INT);
-
INSERT INTO Employee(id,salary) VALUES(1,100);
|
- SELECT MAX(em.salary)
- FROM Employee em
- WHERE em.
|
|