如何根据其他表中支付的最高税率在一个表中获取名称?

时间:2012-12-12 08:39:34

标签: sql postgresql

我有这两张表,

 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。

2 个答案:

答案 0 :(得分:2)

使用此代码。

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

答案 1 :(得分:0)

(刚刚注意到Postgres的标签......这是针对甲骨文的,但无论如何我会把它留下来)

select first_name,
       last_name
from   employees
where  employee_id = (
         select   employee_id
         from     (
                  select   employee_id
                  from     salaries
                  where    salarydate >= add_months(trunc(sysdate,'YYYY'),-12) and
                           salarydate <  trunc(sysdate,'YYYY')
                  group by employee_id
                  order by sum(tax_paid) desc)
         where    rownum = 1)