4

我有这两张桌子,

 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

这里第二个表中的税列是一个月。我想获取去年缴纳最高税的员工的姓名。这里第二个表中的员工 ID 引用了第一个表中的员工 ID。

4

2 回答 2

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
于 2012-12-12T09:03:01.193 回答
0

(刚刚注意到 Postgres 标签……这是给 Oracle 的,但无论如何我都会留下它)

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)
于 2012-12-12T09:44:36.403 回答