Home >Database >Mysql Tutorial >一道sql面试题附答案

一道sql面试题附答案

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:57:261092browse

一道sql面试答案

有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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn