SQL Server:比较同一张表中人的工资,并显示两个名字,有差异

2021-09-10 00:00:00 tsql sql-server

我有下表:

对于演示者

并希望获得如下输出:

示例

我尝试进行内部联接,但觉得有点困惑,因为我对 SQL 还很陌生

I tried doing an inner join, but think got a bit confused as I'm still rather new with SQL

我必须显示同一个表中的 2 个名字,但不确定在哪里放置工资比较,

I got to display 2 names from the same table, but unsure where to place the comparison of the salary,

这是我尝试过的代码:

select 
    t1.PNRFNAME, t2.PNRFNAME, t1.SALARY_YEARLY
from 
    PRESENTERS t1
inner join 
    PRESENTERS t2 on t1.PRESENTER_ID = t2.PRESENTER_ID
order by
    t1.SALARY_YEARLY DESC 

推荐答案

只需添加一个名为 Salary_Diff 的列:

Just add a column called Salary_Diff:

select t1.PNRFNAME,t2.PNRFNAME, t1.SALARY_YEARLY, 

t1.SALARY_YEARLY - t2.SALARY_YEARLY AS Salary_Diff

from PRESENTERS t1
cross join PRESENTERS t2 
where t1.presenterid <> t2.presenterid
ORDER BY t1.SALARY_YEARLY DESC 

相关文章