一道sql面试题附答案
时间:2024-01-18 04:50:33
有1张表,
Wages 表
-------------------------------------------
Emp_id | 基本工资| 工龄工资|
-------------------------------------------
1 | 1.00 | 1.00 |
-------------------------------------------
2 | 1.00 | 2.00 |
-------------------------------------------
3 | 1.00 | 3.00 |
-------------------------------------------
4 | 1.00 | 4.00 |
-------------------------------------------
.........
请从上表用 “一句组合查询” 查询出工资统计表,要求检索出的内容格式如下:
-----------------------------------------------------------------
Emp_id | 基本工资| 工龄工资 | 合计 | 名次
------------------------------------------------------------------
1 | 1.00 | 1.00 |2.00 | x
------------------------------------------------------------------
2 | 1.00 | 2.00 |3.00 | y
------------------------------------------------------------------
3 | 1.00 | 3.00 |4.00 | ..
------------------------------------------------------------------
4 | 1.00 | 4.00 |5.00 | ..
------------------------------------------------------------------
回答:
begin tran
create table Wages(Emp_id bigint not null primary key,基本工资 money, 工龄工资 money)
go
insert into Wages(Emp_id,基本工资,工龄工资)values(1,1.00,1.00)
insert into Wages(Emp_id,基本工资,工龄工资)values(2,1.00,2.00)
insert into Wages(Emp_id,基本工资,工龄工资)values(3,1.00,3.00)
insert into Wages(Emp_id,基本工资,工龄工资)values(4,1.00,4.00)
if @@error>0 rollback else commit tran
select Emp_id,基本工资,工龄工资,基本工资+工龄工资 as 合计,row_number() over(order by 基本工资+工龄工资) as 名次 from Wages order by 合计
--drop table Wages