将薪水与薪水百分比相结合

时间:2014-07-08 07:41:42

标签: sql sql-server

我心中有一个问题......
假设一个部门有三名员工有不同的工资

  

例如: empA = 1000,empB = 1500,empC = 1200 ......

我要求每个员工的两个栏目和总薪水中的薪水百分比

empA 1000 27.03%
empB 1500 40.54%
empC 1200 32.43%

查询可以是什么?

3 个答案:

答案 0 :(得分:4)

请尝试:

select 
    Emp,
    salary,
    CONVERT(numeric(18,2), CONVERT(numeric(18,2), salary)/(
        SUM(salary) over())*100) [Percent]
From tbl

SQL Fiddle Demo

答案 1 :(得分:0)

declare @t table (empid int, empnave varchar(50), salary int)

insert into @t values 
(1,'empA', 1000),
(2,'empB', 1500),
(3,'empC', 1200)

select * from @t 

;with cte
as 
(
select SUM(salary) TotalSalary from @t 
)
select * , cast( salary*100/totalsalary as numeric(18,2)) PercentSalary   from @t , cte

答案 2 :(得分:0)

这样的事情:

SELECT E.Salary, (E.Salary*100)/X.SumSalaries 
   FROM Employee E, (SELECT SUM(Salary) AS SumSalaries FROM Employee) X