题目链接:点击打开链接
题目大意:略。
解题思路:解决方案(2):对于一个奇数 / 偶数长度数组中的中位数,大于这个数的数值个数等于小于这个数的数值个数。最后需要“GROUP BY Company, Salary”是因为子连接时会有左右重复的情况。
AC 代码
--解决方案(1) SELECTid, company, salaryFROM( SELECTid, company, salary, ROW_NUMBER() OVER (PARTITIONBYcompanyORDERBYSalaryASC, idASC) ASrow_num, COUNT(Id) OVER (PARTITIONBYcompany) AScount_idFROMEmployee) WHERErow_numIN (FLOOR((count_id+1)/2), FLOOR((count_id+2)/2)); --解决方案(2) SELECTId, Company, SalaryFROMEmployeeWHEREIdin ( SELECTe1.IdFROMEmployeee1JOINEmployeee2ONe1.Company=e2.CompanyGROUPBYe1.IdHAVINGSUM(CASEWHENe1.Salary>=e2.SalaryTHEN1ELSE0END) >=COUNT(*)/2ANDSUM(CASEWHENe1.Salary<=e2.SalaryTHEN1ELSE0END) >=COUNT(*)/2 ) GROUPBYCompany, SalaryORDERBYCompany;