sql – 如何根据其他表中支付的最大税额在一个表中获取名称?

我有这两张桌子,

 employeeid | firstname  | lastname |   address   | pan  |  joindate  | lastupdatedate | annualincome | taxrate | currentgrade 
------------+------------+----------+-------------+------+------------+----------------+--------------+---------+--------------
          1 | tushar     | mishra   | bommanhalli | ab7c | 2012-10-15 |                |       300000 |    0.05 |            2
          2 | puneet     | purohit  | j.p         | ab5c | 2012-10-15 |                |       300000 |    0.05 |            2
          3 | vishwanath | b.s      | btm         | ab6c | 2012-10-15 |                |       300000 |    0.05 |            1
          4 | xavier     | d'souza  | btm         | ab8c | 2012-10-15 |                |       300000 |    0.05 |            1
          5 | deepak     | kumar    | hebbal      |      | 2012-10-15 |                |       300000 |    0.05 |            1

和其他一个..

employeeid | salarydate | income | tax  
------------+------------+--------+------
          2 | 2012-11-01 |  25000 | 1250
          3 | 2012-11-01 |  25000 | 1250
          4 | 2012-11-01 |  25000 | 1250
          5 | 2012-11-01 |  25000 | 1250
          2 | 2012-12-01 |  25000 | 1250
          3 | 2012-12-01 |  25000 | 1250
          4 | 2012-12-01 |  25000 | 1250
          5 | 2012-12-01 |  25000 | 1250
          2 | 2013-01-01 |  25000 | 1250
          3 | 2013-01-01 |  25000 | 1250
          4 | 2013-01-01 |  25000 | 1250
          5 | 2013-01-01 |  25000 | 1250
          1 | 2012-11-01 |  25000 | 1500
          1 | 2012-12-01 |  25000 | 1500
          1 | 2013-01-01 |  25000 | 1500

这里第二个表中的税柱是一个月.我想获取去年缴纳最高税的员工的姓名.这里第二个表中的employee id引用第一个表中的employeid.

最佳答案
使用此代码.

select salary.employeeid,firstname,lastname, sum(tax) 
 from salary
left join employee 
   on salary.employeeid=employee.employeeid
group by salary.employeeid,firstname,lastname
order by sum(tax) DESC LIMIT 1

转载注明原文:sql – 如何根据其他表中支付的最大税额在一个表中获取名称? - 代码日志