增加%SQL中的金额

时间:2017-02-09 06:38:38

标签: sql sql-server tsql percentage

select sr.id,sr.increasedamount,sr.newsalary
    from revision sr
    inner join vwemp vw on vw.id=sr.id

id      increasedamount newsalary   
15691   14600           80000   
12500   2236            26000   
12501   1969            25500   
1252    1982            25000

现在在这里我要%显示在第一个记录金额中增加的百分比是14600 所以我想显示新列增加14%

id      increasedamount newsalary    increment in %
15691   14600           80000   
12500   2236            26000   
12501   1969            25500   
1252    1982            25000

4 个答案:

答案 0 :(得分:1)

select sr.id,sr.increasedamount,sr.newsalary,
CASE WHEN sr.newsalary-sr.increasedamount=0 THEN 0 
ELSE (sr.increasedamount)*(100/CONVERT(decimal,(sr.newsalary-sr.increasedamount))) END as 'incrementinpercentage'
from revision sr
inner join vwemp vw on vw.id=sr.id

答案 1 :(得分:0)

你可能正在寻找这个

SELECTC id,
        increasedamount,
        newsalary,
        ((increasedamount*1.0)/(newsalary-increasedamount))*100 
FROM revision 

答案 2 :(得分:0)

如果您需要获得比现有金额高22%的金额,请将其乘以1.22。如果需要将其舍入为特定值,请对结果使用round函数以获得所需的零值。 round(salary,-2)将舍入到最接近的百位。例如,如果对于ID = 1,old_salary为100美元,则下面将导致1,$ 122,$ 22

select sr.id, (OLD_SALARY*1.22) NewSALARY, (OLD_SALARY*1.22 -OLD_SALARY)Increasedamount from table

如果你只想让你的结果看起来只有22%而不是.22,你需要做类似的事情     cast(增量* 100为varchar)+'%'

答案 3 :(得分:0)

Below query will give you expected results

declare @userData TABLE(id int,increasedamount int,newsalary int)
insert into @userData values(15691,14600,80000)
insert into @userData values(12500,2236,26000)
insert into @userData values(12501,1969,25500)
insert into @userData values(1252,1982,25000)

select id,increasedamount,newsalary,(case when previousSalary > 0 then (increasedamount *100/previousSalary) else 0 end) incrementPercentage
from (select id,increasedamount,newsalary,(newsalary - increasedamount) previousSalary from @userData) as t