sql刷题

262. Trips and Users 难题

case when 记得加end, between如果不是数字的话记得加引号

答案:为了把users里面的client和driver分离 用了 两个表

select Request_at as 'Day',

    round(sum(case when Status like 'cancelled%' then 1 else 0 end)/count(Status),2) as 'Cancellation Rate'

    from Trips ,Users as Users_client, Users as Users_driver

    where Users_client.Banned='No'

    and Users_client.Role='client'

    and Users_client.Users_Id=Trips.Client_Id

    and Users_driver.Banned='No'

    and Users_driver.Role='driver'

    and Users_driver.Users_id=Trips.Driver_id

    and Request_at between '2013-10-01' and '2013-10-03'

    group by Request_at

185. Department Top Three Salaries 难题

这是一个分组 组内排序 top n的问题

https://blog.csdn.net/wguangliang/article/details/50167283这个文章说的挺好的

select D.Name as Department, E.Name as Employee, Salary from Employee as E join Department as D on E.DepartmentId=D.Id where 3>(select count(distinct(Salary)) from Employee where Employee.DepartmentId=E.DepartmentId and Employee.Salary>E.Salary) order by Department, Salary desc

SELECT d.Name as Department, e1.Name as Employee, e1.Salary FROM Department d, Employee e1, Employee e2 WHERE d.ID = e1.DepartmentId and e1.DepartmentId = e2.DepartmentId and e1.Salary <= e2.Salary group by d.ID,e1.Name having count(distinct e2.Salary) <= 3 order by d.Name, e1.Salary desc

601. Human Traffic of Stadium 难题

select * from stadium where id in (select s1.id from stadium s1, stadium s2, stadium s3 where s1.id=s2.id+1 and s2.id=s3.id+1 and s1.people>=100 and s2.people>=100 and s3.people>=100 union select s2.id from stadium s1, stadium s2, stadium s3 where s1.id=s2.id+1 and s2.id=s3.id+1 and s1.people>=100 and s2.people>=100 and s3.people>=100 union select s3.id from stadium s1, stadium s2, stadium s3 where s1.id=s2.id+1 and s2.id=s3.id+1 and s1.people>=100 and s2.people>=100 and s3.people>=100)

626. Exchange Seats 用了coalesce 幂函数用power 中等题

select s1.id, coalesce(s2.student,s1.student) as student from seat s1 left join seat s2 on s2.id=s1.id-power(-1,s1.id%2) order by s1.id

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,121评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,765评论 18 399
  • solr是什么 Solr is highly reliable, scalable and fault toler...
    Uncle阳阅读 1,796评论 0 0
  • 决定从今天开始每天中午写半个小时的日记,记录工作或者生活中的小事情、小心情,积攒自己的生活之书。 ——写在前面的话...
    泰然微语阅读 441评论 0 1
  • 临江仙.滚滚长江东逝水 (明)杨慎 滚滚长江东逝水, 浪花淘尽英雄。 是非成败转头空, 青山依旧在,几度夕阳红。 ...
    我是宣阅读 437评论 9 17