4

i have these two tables,

 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

and other one..

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

here the tax column in second table is for a month .i want to fetch the name of the employee who had paid highest tax last year. here employee id in second table is referenced to the employeid in 1st table.

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 に答える