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:比较同一张表中人的工资,并显示两个名字,有差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SnowFlake Diagram and Many to Many relationship(雪花图与多对多关系)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)