我有一个需要编写的查询。实验室助理将输入实验室寻求帮助的学生的信息。该领域是:
Visit_ID,Student_ID,Visit_Date,Time_In,Time_Out,Course,Asst_ID。
time_in条目有3个会话
Session 1 9-12
Session 2 12-3
Session 3 3-6
查询需要确定当天哪个会话访问者最多。这就是我到目前为止所做的:
with cte as
(
select
case
when cast(Time_In as time) >'12:00:00' and cast(Time_In as time) <='15:00:00' and Visit_Date = cast(GETDATE() as date)then 'Session 2'
when cast(Time_In as time) >'3:00:00' and cast(Time_In as time)<= '6:00:00' and Visit_Date = cast(GETDATE() as date) then 'Session 3'
else 'Session 1'
end session
/*course*/
from Lab_Visits2
), ctecnt as (
select session,/* course,*/ count(*) cnt
from cte
group by session /*course*/
)
select session, /*course,*/ (cnt)
from (
select session, /*course,*/ cnt, row_number() over (order by cnt desc) rn
from ctecnt
) t
where rn = 1