从单个列中的值的条件中选择具有多行的语句,从而影响多个列

时间:2014-06-19 09:20:03

标签: sql conditional-statements

我有下表。使用薪水作为条件我想获得多行。下面是当前的表呼叫员工。

   empid |    name    |     salary
-----------------------------------
1   A1        alex        20000
2   B2        ben         4500
3   C1        carl        14000

将薪水与某些固定值进行比较,每当薪水大于固定值时,在输出中显示一条记录。还输出一个计算的税柱。下面是我最后结果的一步

select e.*,tax= (case when salary<6000 then tax=0.06 *salary,when salary between 6000 and 18000 then tax= 0.06 *(salary -6000),else tax =0 ),m.incometype,
from employee e
left join 
( 
 select 0 as threshold, 101 as incometype
 union
 select 5999 as threshold, 102 as incometype
 union
 select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid

所需的输出将是:

   empid |    name |  salary | incometype | tax
----------------------------------------------
1   A1        alex    20000    101       360
2   A1        alex    20000    102       720 
3   A!        alex    20000    103       0 
4   B2        ben     4500     101       270
5   C1        carl    14000    101       360
6   C1        carl    14000    102       480

这是Select statement with multiple rows from condition on values in single column

的进一步问题

1 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助

select e.*,
case 
when salary<6000 then (0.06*salary) 
when salary between 6000 and 1800 then (0.06*(salary -6000))
when m.incometype=103 then 0 
end as tax
,m.incometype,
from employee e
left join 
( 
 select 0 as threshold, 101 as incometype
 union
 select 5999 as threshold, 102 as incometype
 union
 select 17999 as threshold, 103 as incometype
) m
on e.salary > m.threshold
order by e.empid