(有多少用户名包含字母A?)
select Username
from Customer
where Username like 'A%'
2.What is the average journey length, in seconds? (5)
(以秒为单位的平均旅程长度是多少)
select (sum (datediff(second, StartTime, EndTime))/ lenght.port)averjour from journey ,(
select sum (abs(convert (int, Journey.EndStation) –
convert(int, Journey.StartStation)))as port
from Journey
) as lenght group by lenght.port
3. List the stations on the Eastern line (excluding City),in outbound order. (5)
(按出站顺序列出东线(不含市区)站)
select Name
from Station
where Sequence !=0 and line=(
select Id
from line
where Name='Eastern')
order by Sequence ASC
4. On which day of the week (Monday, Tuesday etc.) are the most journeys made? (5)
(每星期哪一天(星期一、星期二等)是旅行次数最多的一天)
select
top 1
COUNT (*) Number,bb.aa
from(
select datename(DW, Journey.StartTime) as aa
from Journey
) as bb
group by bb.aa
order by Number DESC
5. List any stations at which no passenger has started or ended a journey. Show the station id and name. (10)
(列出任何没有乘客开始或结束旅程的车站,显示站点id和名称)
select Id,Name
from Station
where Id in(
select Id
from Station
except(
select distinct Journey.StartStation
from Journey
union
select distinct Journey.EndStation
from Journey
)
)
6. List the details of each journey, along with its cost. (10)
(列出每个旅程的细节,以及费用)
7. List the station ids, along with the number of journeys that started or stopped at each station. (10)
(列出站点id,以及每个站点开始或停止的旅程数量。)
select aa.Id Id,COUNT (*) Number
from Journey join (
select Id
from Station
group by Id
) as aa on Journey.StartStation = aa.Id
or Journey.EndStation = aa.Id
group by aa.Id
8. List the journeys that ended at the last station on the line.(the station with the highest sequence number for a line) (15)
(列出最后一站结束的旅程(线路序号最高的车站))
select *
from Journey
where Journey.endStation = (select MAX(Id)
from Station
where line in(
select Id
from line
where Name='Northern'))
OR Journey.endStation = (select MAX(Id)
from Station
where line in(
select Id
from line
where Name='Western'))
OR Journey.endStation = (select MAX(Id)
from Station
where line in(
select Id
from line
where Name='Eastern'))
208Rows Returned
9. For each journey, show how many stations it passed through.(Count the end station but not the start station.) (15)
(每一个旅程,显示它经过了多少站,终点,但不要数起点))
select Id,abs((EndStation-StartStation))number
from Journey
10. List the usernames of customers who have travelled on all lines. (20)
(列出所有行的客户的用户名。)
select Username
from Customer
where Id in(
select Customer
from Journey
where StartStation<=(select max(Id)
from Station
where line in(
select Id
from line
where Name='Northern')
)and EndStation>=(select min(Id)
from Station
where line in(
select Id
from line
where Name='Eastern'))
)