我有一张这样的表:
NAME SALARY
Gareth 37451000.7
Lorah 2426222.55
Sage 2910000.98
我想找出每位员工的工资差异,按最高差异排序?像这样......
NAME name salary diff
Sage Lorah 483778.43
Sage Gareth -34540999.72
Lorah Gareth -35024778.15
如何编写此结果?感谢。
答案 0 :(得分:3)
根据您的要求,这里有一个解决方案
declare @report table(Id int identity(1,1),
Name varchar(20),
Salary money)
insert into @report(Name,Salary)
select 'Gareth' as Name,37451000.7 as Salary
union all
select 'Lorah' as Name,2426222.55 as Salary
union all
select 'Sage' as Name,2910000.98 as Salary
select p.Name,r.Name as SName, p.Salary - r.Salary as [Diff]
from @report r
inner join @report p on r.Name <> p.Name and r.Id<p.Id
这里是输出结果
Name SName Diff
Lorah Gareth -35024778.15
Sage Gareth -34540999.72
Sage Lorah 483778.43
最好的问候
答案 1 :(得分:1)
SELECT
a.Name
,b.Name
,a.salary - b.salary as 'Salary Difference'
FROM #salary a
JOIN #salary b
ON a.name != b.name
AND b.id < a.id