惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - bobomouse

关于TFS2010 远程无法创建团队项目的若干问题总结 QCon北京2012讲义下载 Tech.Ed 2011 微软技术大会资料下载 【转】Data Mining 电子书下载 关于CellSet转DataTable的改进方案 FusionChart对MDX查询结果的数据展示案例 2011高校信息化实践者精英论坛之上海交大BI系统汇报 28个强大的数据可视化工具(转) 关于BI产品的选型(BI产品对比) 国内外BI厂商名录下载 怀恋Dundas系列控件 微软Tech•Ed 2009日程及相关大会ppt下载地址 【转】微软早期员工因何机缘加盟微软? [转]【收藏】一些常用的WebServices 【每日一得】关于对SQL Server 2008用PowerDesigher进行逆向工程的问题 一个关于SOA的调查 UMLChina历次专家讲座录音及幻灯 SOA之我见(转) 我的选题——数据挖掘技术和SOA技术结合
一个外企SQL面试题的困惑
bobomouse · 2008-03-25 · via 博客园 - bobomouse

    无意间在网上看到一个外企的SQL面试题,请大家来讨论何解?
    题目如下:

Your Name: 

Given the following tables:

Refuel

CarID

OdometerReading

LitersGas

1

1245

55.25

1

1457

16.96

1

1872

34.23

1

2193

25

1

2448

20.4

1

2508

5.52

1

2663

14.15

1

3075

41.82

1

3387

27.77

2

112145

36

2

112972

34.24

2

113357

30.03

2

113731

29.92

2

114130

36.7

2

114535

36.98

2

114943

41.41

3

18091

30.25

3

18291

16.6

3

18506

18.27

3

18791

23.37

3

19065

22.74

3

19364

28.40

3

19569

18.65

CAR

CarID

Color

Make

Model

1

Blue

Ford

Taurus

2

Green

VW

Jetta

3

Blue

Acura

1.7EL

Take it as a given that every time a car is refueled, the tank is filled.

To determine gas consumption (l/100km), take the LitersGas, divide it by kilometers traveled, and multiply by 100.

For example:

If the car takes 45 liters of gas, and had traveled 502 km since the previous time it was refueled, the fuel consumption would be: 45 liters / 502 km * 100 = 8.96 l/100km

1. Is it possible to make a single sql statement that would determine the fuel consumption and return the following table:

OdometerReading

Consumption

Color

Make

1245

n/a

Blue

Ford

1457

7.54

Blue

Ford

1872

8.19

Blue

Ford

2193

7.78

Blue

Ford

2448

7.84

Blue

Ford

2508

8.33

Blue

Ford

2663

9.03

Blue

Ford

3075

9.95

Blue

Ford

3387

8.65

Blue

Ford

112145

n/a

Green

VW

112972

4.11

Green

VW

113357

7.79

Green

VW

113731

7.75

Green

VW

114130

9.02

Green

VW

114535

8.88

Green

VW

114943

10.04

Green

VW

18091

n/a

Blue

Acura

18291

8

Blue

Acura

18506

8.37

Blue

Acura

18791

8.07

Blue

Acura

19065

8.02

Blue

Acura

19364

9.36

Blue

Acura

19569

8.78

Blue

Acura

2. If you answered yes to question 1, please give the sql statement.

If you answered no, please explain why it isn’t possible, what extra information is needed and give a sample sql statement that should give the answer above.

我在SQL SERVER 2000下测试了一下,不知道是没有理解题目意思还是说题目给出的答案有问题,反正就是没对上。请大家来评评:


use tempdb
create table #Refuel
(
 CarID int,
 OdometerReading int,
 LitersGas numeric(4,2)
)
go

create table #car
(
 CarID int,
 Color varchar(200),
 Make varchar(200),
 Model varchar(200)
)
go

insert into #car values(1,'Blue','Ford','Taurus')
insert into #car values(2,'Green','VW','Jetta')
insert into #car values(3,'Blue','Acura','1.7EL')


insert into #Refuel values(1,1245,55.25)
insert into #Refuel values(1,1457,16.96)
insert into #Refuel values(1,1872,34.23)
insert into #Refuel values(1,2193,25)
insert into #Refuel values(1,2448,20.4)
insert into #Refuel values(1,2508,5.52)
insert into #Refuel values(1,2663,14.15)
insert into #Refuel values(1,3075,41.82)
insert into #Refuel values(1,3387,27.77)
insert into #Refuel values(2,112145,36)
insert into #Refuel values(2,112972,34.24)
insert into #Refuel values(2,113357,30.03)
insert into #Refuel values(2,113731,29.92)
insert into #Refuel values(2,114130,36.7)
insert into #Refuel values(2,114535,36.98)
insert into #Refuel values(2,114943,41.41)
insert into #Refuel values(3,18091,30.25)
insert into #Refuel values(3,18291,16.6)
insert into #Refuel values(3,18506,18.27)
insert into #Refuel values(3,18791,23.37)
insert into #Refuel values(3,19065,22.74)
insert into #Refuel values(3,19364,28.4)
insert into #Refuel values(3,19569,18.65)

select a.OdometerReading,Convert(numeric(4,2),a.LitersGas/(a.OdometerReading-(select top 1 c.OdometerReading from #Refuel c where c.carid=a.carid and c.OdometerReading<a.OdometerReading order by c.OdometerReading desc))*100) as 'Consumption', b.Color,b.Make from #Refuel a inner join #car b on a.carid=b.carid

drop table #Refuel

drop table #car

 得到的结果如下:

OdometerReading

Consumption

Color

Make

1245

NULL

Blue

Ford

1457

8.00

Blue

Ford

1872

8.25

Blue

Ford

2193

7.79

Blue

Ford

2448

8.00

Blue

Ford

2508

9.20

Blue

Ford

2663

9.13

Blue

Ford

3075

10.15

Blue

Ford

3387

8.90

Blue

Ford

112145

NULL

Green

VW

112972

4.14

Green

VW

113357

7.80

Green

VW

113731

8.00

Green

VW

114130

9.20

Green

VW

114535

9.13

Green

VW

114943

10.15

Green

VW

18091

NULL

Blue

Acura

18291

8.30

Blue

Acura

18506

8.50

Blue

Acura

18791

8.20

Blue

Acura

19065

8.30

Blue

Acura

19364

9.50

Blue

Acura

19569

9.10

Blue

Acura