增加百分比 SQL

Increase amount in % SQL

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
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

您可能正在寻找这个

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

如果您需要获得比现有金额高 22% 的金额,只需将其乘以 1.22。如果您需要将其舍入为特定值,请对结果使用舍入函数以获得所需的零数。 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)+ '%'

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