subway

嗯嗯嗯~参考答案不一定正确哈~题意理解的不一定正确哈~英译中可能改变了语言原本的想要表达的意思~参考的时候注意一些,慎用哦~

 

本题的题目描述以及本题所要用到的数据库请前往我的资源里面去下载哦~

下载subway数据库,city_subway_system这两个文件哈~

嗯~本来想把题搞到这上面来的,但是排版有点难看,所以想要学习的小伙伴还是麻烦多走几步吧~

参考答案是博主本人的理解,有兴趣的小伙伴欢迎指出错误,我想听听你们的想法~

 

  1. How many usernames contain the letter A? (5)

(有多少用户名包含字母A?)

select Username

from Customer

where Username like 'A%'

subway_第1张图片             3 Rows Returned

 

 

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

           1 Rows Returned

 

 

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

subway_第2张图片      3 Rows Returned

 

 

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

            1 Rows Returned

 

 

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

       )

)

subway_第3张图片              2Rows Returned

 

 

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

subway_第4张图片                  12Rows Returned

 

 

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'))

subway_第5张图片

                   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

subway_第6张图片              600Rows Returned

 

 

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'))

    )

subway_第7张图片        5Rows Returned

 

你可能感兴趣的:(数据库学习笔记)