【发布时间】:2021-12-02 10:39:28
【问题描述】:
我见过其他解决方案。很好奇这种解决这些问题的方法是否可行。
表格:
attendance_events : 日期 |学生ID |出席率
所有学生:学生 ID |学校ID |等级等级 |出生日期 |故乡
有多少百分比的学生在生日那天上学?
With agg_join as (SELECT att.date as dates, att.attendance as attendance, als.date_of_birth as DOB, att.student_id as student_id
FROM attendance_events att
join all_students als on att.student_id = als.studentid)
Select count(DISTINCT student_id) as total_students,
count( Distinct case when DOB = dates and attendance = TRUE) as count_of_DOBS,
total_students/ count_of_DOBS as percent_of_student
from agg_join
从昨天到今天,哪个年级的出勤率下降幅度最大?
With agg_join as ( SELECT att.date as dates, att.attendance as attendance, als.grade_level as grade
FROM attendance_events att
join all_students als on att.student_id = als.studentid)
Select grade,
case when dates ( 'd', -1, currentdate) and attendance = True then 1
else 0 end as yesterday_att,
case when dates ( 'd', currentdate) and attendance = True then 1
else 0 end as Today_att,
(Today_att - yesterday_att) * -1 AS DIFF
from agg_join
Group by grade
Order by DIFF DESC
Limit 1
【问题讨论】:
-
在我看来,您可以自己运行这些以查看它们是否有效。您是否要求我们在提交之前检查您的作业?这是一个问答网站。请注意,question 是单数,而不是复数。你问了两个问题,这两个问题你似乎都已经回答过了。在您自己执行了这两个解决方案之后,您就会知道它们是否有效,并且您的讲师将决定这是否是他们正在寻找的答案。
-
@KenWhite 感谢您的回复。这不是针对硬件的,而是针对面试练习假设性问题。如果您能指出我可以在哪里运行此代码的方向,我真的不知道我可以在哪里执行此操作(我一直在使用网站来学习如何使用 SQL)。所以我可以使用一些指针。感谢您的帮助和先发制人的推理。
-
您可以使用像dbfiddle.uk/?rdbms=postgres_13 这样的小提琴网站进行测试。
-
我无法告诉您可以在哪里运行 SQL,因为您尚未确定正在使用的 DBMS。 (显然不是 MySQL 和 PostgreSQL,因为如果你在这里问这个问题,你的经验不足以同时使用两者。)我们也不推荐这里的异地服务,所以告诉你在哪里可以找到这样的网站将违反网站指南。如果您花一些时间在tour 和阅读help center 页面以了解该网站的工作原理,然后再开始发布,您会发现您在这里的体验会好很多。
-
就我的推定推论而言,如果它像鸭子一样走路,像鸭子一样游泳,像鸭子一样嘎嘎......我们确实看到了成千上万的作业每年这个时候一个月左右的问题,根据您发布的确切内容,没有理由认为这不是另一个问题。
标签: sql postgresql join aggregate-filter