📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: crystalcc
跳转到指定楼层
上一主题 下一主题
收起左侧

SQL刷题记录 求战友互相督促交流

 
🔗
 楼主| crystalcc 2019-9-6 01:45:49 | 只看该作者
全局:
580. Count Student Number in Departments
select dept_name, student_number
from department d join
(select dept_id, ifnull(count(distinct student_id) over(partition by dept_id), 0) as student_number from student) num. 1point 3acres
on d.dept_id = num.dept_id
order by student_number desc, dept_name ..

select dept_name, student_number.google  и
from department d join . ----
(select dept_id, ifnull(count(distinct student_id), 0) as student_number from student group by dept_id) num. Χ
on d.dept_id = num.dept_id
order by student_number desc, dept_name

/*TODO: 即使不ifnull,只要做了left join就可以正确输出student_number = 0的情况,为什么??*/
select dept_name, ifnull(count(distinct student_id), 0) as student_number. Waral dи,
from department d join student
on d.dept_id = student.dept_id
group by dept_name
order by student_number desc, dept_name
回复

使用道具 举报

🔗
 楼主| crystalcc 2019-9-7 05:55:06 | 只看该作者
全局:
584. Find Customer Referee
select name. From 1point 3acres bbs
from customer
where referee_id is null or referee_id != 2
回复

使用道具 举报

🔗
 楼主| crystalcc 2019-9-10 14:58:18 | 只看该作者
全局:
585. Investments in 2016
select sum(round(tiv_2016, 2)) as tiv_2016
from insurance a
-baidu 1point3acreswhere (select count(1) from insurance b where a.lat = b.lat and a.lon = b.lon) = 1 and (select count(1) from insurance c where a.tiv_2015 = b.tiv_2015) > 1
. 1point 3acres
/*TODO: 结果不正确。按理说不是需要同时满足same tiv_2015和不同city这两个条件么,一起and做筛选为何不可以?*/
select sum(round(tiv_2016, 2)) as tiv_2016
from insurance.1point3acres
where pid in
(select i.pid
from insurance i
where exists
(select 1 from insurance i1 where i.pid != i1.pid and i.tiv_2015 = i1.tiv_2015 and i.lat != i1.lat and i.lon != i1.lon))

/*修改为以下代码就对了*/
select sum(round(tiv_2016, 2)) as tiv_2016. From 1point 3acres bbs
from insurance a
where exists
(select 1 from insurance b where a.pid != b.pid and a.tiv_2015 = b.tiv_2015)
and not exists
(select 1 from insurance c where a.pid != c.pid and a.lat = c.lat and a.lon = c.lon). 1point 3acres
回复

使用道具 举报

🔗
may8889 2019-9-13 23:33:45 | 只看该作者
全局:
马一个,楼主有心了!加米!
回复

使用道具 举报

🔗
 楼主| crystalcc 2019-9-14 15:15:11 | 只看该作者
全局:
may8889 发表于 2019-9-13 23:33
马一个,楼主有心了!加米!

haha thanks! 真的很需要米。。。quq
回复

使用道具 举报

🔗
 楼主| crystalcc 2019-9-17 02:35:32 | 只看该作者
全局:
Hackerrank weather observation station 20 求中位数

SELECT ROUND(lat_n,4) AS median
FROM station s. check 1point3acres for more.
WHERE (SELECT COUNT(lat_n) FROM STATION WHERE lat_n > s.lat_n) = (SELECT COUNT(lat_n) FROM STATION WHERE lat_n < s.lat_n)    . .и

几个细节:
1.  >, not >=. 1point 3acres
2. count lat_n,not count * group by lat_n
3. 在两个subquery内比较,所以只alias for最终呈现的table即可.
回复

使用道具 举报

🔗
 楼主| crystalcc 2019-9-17 03:00:11 | 只看该作者
全局:
hackerrank the report
. 1point 3acres
SELECT CASE WHEN g.grade > 7 THEN s.name ELSE NULL END AS name, g.grade, s.marks
FROM students s, grades g.1point3acres
WHERE s.marks BETWEEN g.min_mark AND g.max_mark.--
ORDER BY grade DESC, COALESCE(name, marks)
/*trick在于如何实现当grade>7时按name排序,当grade>7时按marks排序。原本我用了where+union,但其实利用null的条件,coalesce/ifnull即可解决*/
回复

使用道具 举报

🔗
 楼主| crystalcc 2019-9-25 04:05:48 | 只看该作者
全局:
601. Human Traffic of Stadium
select s1.*
from stadium s1, stadium s2, stadium s3
where s1.people >= 100 and s2.people >= 100 and s3.people >= 100. .и
and ((s1.id = s2.id - 1 and s1.id = s3.id - 2)
or (s1.id = s2.id + 1 and s1.id = s3.id - 1)
or (s1.id = s2.id + 2 and s1.id = s3.id + 1))
group by s1.id

/*This one beats 100% MS SQL online submissions 骄傲地发了个discussion嘻嘻*/.google  и
select id, visit_date, people
from
(select *,
lead(people) over(order by id) as after1,
lag(people) over(order by id) as before1,
lead(people, 2) over(order by id) as after2, .--
lag(people, 2) over(order by id) as before2
from stadium) temp
where people >= 100 and (
(before1 >= 100 and after1 >= 100)
or (after1 >= 100 and after2 >= 100)
or (before1 >= 100 and before2 >= 100))
回复

使用道具 举报

🔗
zouma-spec 2022-10-5 10:34:24 | 只看该作者
全局:
我才开始刷题 搜面经题感觉像天书一样 学的跟考的也差太多了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表