Sql易错题

就写了几道自己错过的题目:

1、查找Employee中薪水第五高的薪水金额,有的话显示金额,没有的话显示null

2、给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
在这里插入图片描述
3、小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
在这里插入图片描述
更改之后的结果为
在这里插入图片描述
4、X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (date)、 人流量 (people)。

请编写一个查询语句,找出高峰期时段,要求连续三天及以上,并且每天人流量均不少于100。
在这里插入图片描述
5、分组查询
例子表结构数据如下:

id status date price

1 1 2013-10-01 218

2 1 2013-10-02 218

3 0 2013-10-03 218

4 0 2013-10-04 238

5 0 2013-10-05 238

6 0 2013-10-06 238

7 0 2013-10-07 258

8 0 2013-10-08 258

9 0 2013-10-09 218

想获取的结果集一:

2013-10-01至2013-10-03 218

2013-10-04至2013-10-06 238

2013-10-07至2013-10-08 258

2013-10-09至2013-10-09 218

想获取的结果集二:

1 2013-10-01至2013-10-02 218

0 2013-10-03至2013-10-03 218

0 2013-10-04至2013-10-06 238

0 2013-10-07至2013-10-08 258

0 2013-10-09至2013-10-09 218

(3)、 根据实际所需规格table去比对另一个库存table取浪费最少的数据

情境描述:根据表A里的物料去比对表B,然后表A根据A1括号里两个尺寸浪费最少来将A1替换成最省的物料。

表A用量需求表:Table A

A0(自增长ID) A1(物料编号)


0 ls001-(900*110)

1 ls002-(200*300)

表B库存物料表: B1没有重复,可以当作ID来使用 Table B:

B1(库存物料) B2(规格1) B3(规格2)


ls001-(700*200) 700 200

ls001-(910*140) 910 140

ls001-(920*120) 920 120

… … …

ls002-(100*200) 100 200

ls002-(200*350) 200 350

ls002-(220*320) 220 320

原理是:ls001取(920120)的话浪费分别是左边920-900=20,右边120-110=10,总共浪费是30, 是ls001库存规格(700200),(910140),(920120)里浪费最少的,ls002同理。

最后A1字段被替换后的效果如下:

A0(自增长ID) A1(物料编号)


0 ls001-(920*120)

1 ls002-(220*320)

答案

1、select ISNULL(( select salary from ( select *,RANK()over(order by Salary desc) rank from Employee)as A where A.rank=5),null) as Salary

2、

select W1.ID from Weather as W1,Weather as W2 where W1.Temperature>W2.Temperature and 
     W1.RecordDate=DATEADD(DAY,1,W2.RecordDate)

3、

select ID-1 as id ,student from seat where id%2=0 union
     select ID+1 as id,student from seat where id%2=1 and id<(select COUNT(*) from seat) union
     select * from seat where id=(select COUNT(*) from seat) and id%2=1

4、

 select s4.* from stadium as s1,stadium as s2,stadium as s3 ,stadium as s4
         where s1.date=dateadd(DAY,1,s2.date) and s2.date=DATEADD(DAY,1,s3.date)
          and s1.people>100 and s2.people>100 and s3.people>100 and s4.id in(  s1.id,s2.id ,s3.id )

5、

(1)使用with as 建立临时表

  with t as (
    
    select *,RANK()over(partition by price order by id) as rank,min(id)over(partition by price) as min_id from data),
    
    a
    as
    (select *,RANK-(ID-min_id) as intervel from t )
    select CAST(MIN(date) as varchar(20)) + '至' + CAST(MAX(date) as varchar(20)) as date,price from a group by price,intervel
    order by date

(2)

with t as 
 (select *,RANK()over(partition by price,status order by id )as rank,MIN(id)over(partition by price,status) as min_id from data),
 tt as 
 (select *,RANK-(ID-min_id) as inter from t)
 select status,CAST(MIN(date) as varchar(20)) + '至' + CAST(MAX(date) as varchar(20)) as date,price from tt group by price,status,
 inter order by 1 desc,date

(3)

 with tt as
(select A0,A1,CHARINDEX('(',A1)as sta1,CHARINDEX('*',A1) as sta2,CHARINDEX(')',A1) as sta3 from A),
t as
(select A0,A1,sta1,sta2,sta3,(sta2-sta1-1)as lens1,(sta3-sta2-1)as lens2 from tt),
m as
(select A0,A1,substring(A1,1,sta1-1) as num,cast(SUBSTRING(A1,sta1+1,lens1) as numeric) as q1,cast(SUBSTRING(A1,sta2+1,lens2) 
as numeric) as q2 from t),
mm as
(select CHARINDEX('-',B1) as s,B1,B2,B3 from B),
m2 as
(select SUBSTRING(B1,1,s) as num,B1,B2,B3 from mm),
m3 as
(select A0,A1,B1,B2,B3,(abs(q1-B2)+abs(q2-B3)) as ss ,m2.num from m2 left join m on m2.num = m.num),
m4 as
(select A0,MIN(ss) as s from m3 group by A0)
select m4.A0,B1 from m4 left join m3 on m4.A0=m3.A0 and m4.s=m3.ss 

猜你喜欢

转载自blog.csdn.net/weixin_43408956/article/details/88121068