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

SQL Server: compare salary of people in the same table, and display both names, with the difference(SQL Server:比较同一张表中人的工资,并显示两个名字,有差异)
本文介绍了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 

这篇关于SQL Server:比较同一张表中人的工资,并显示两个名字,有差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Dynamically group by with quot;Group Headerquot;(使用“组头动态分组)
How to retrieve leaf path in parent-child table in SQL Server with root ID?(如何使用根 ID 在 SQL Server 中的父子表中检索叶路径?)
Tsql union in while loop(while循环中的Tsql联合)
Pass a column as parameter to dateadd in SQL Server(将列作为参数传递给 SQL Server 中的 dateadd)
Is it possible to convert rows to a variable number of columns in T-SQL?(是否可以在 T-SQL 中将行转换为可变数量的列?)
Swap values between two rows of data(在两行数据之间交换值)