语法问题?

时间:2015-08-11 09:34:19

标签: sql join count

我使用的系统不允许我直接访问SQL数据库,我必须使用他们内置的报告工具,这很糟糕。我正在运行下面的代码并且它告诉我附近有语法错误但是我无法看到自己这是不正确的,有什么想法吗?

SELECT ST.Staff_ID,
COUNT (CASE WHEN Referral_Date BETWEEN @StartDate AND @EndDate ) AS 'Referrals',
COUNT (CASE WHEN Att_Start_Dttm BETWEEN @StartDate AND @EndDate ) AS 'Appointments'

FROM tbldiaryappointment DA
left outer join tblDiaryAppointmentClinicianInvitee CI on DA.Diary_Appointment_ID = CI.Diary_Appointment_ID
left outer join tblstaff ST on CI.Clinician_Invitee_Staff_ID = ST.STaff_ID
left outer join tbllocation LOC on LOC.Location_ID = DA.Scheduled_Location_ID
left outer join tblpatient PAT on PAT.Patient_ID = DA.Patient_ID
left outer join tblAttendanceTypeValues ATV on ATV.Attendance_Type_ID = DA.Attendance_Type_ID
left outer join tblDiaryAppointmentScheduledEvent SE on SE.Diary_Appointment_ID = DA.Diary_Appointment_ID
left outer join tblEventValues EVE on EVE.Event_ID = SE.Event_ID
left outer join tblProfessionalGroupValues PGV on ST.Professional_Group_ID = PGV.Professional_Group_ID
left outer join tblStaffJobRoleCodeValues JOB on JOB.Staff_Job_Role_Code_ID = ST.Job_Role_Code_ID
left outer join tblReferral REF on REF.Patient_ID = DA.Patient_ID

1 个答案:

答案 0 :(得分:3)

搜索CASE表达式的语法是:

CASE
     WHEN Boolean_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END

您在Boolean_expression之后错过了该部分。

此外,您的陈述缺少GROUP BY条款。

SELECT ST.Staff_ID,
COUNT (CASE WHEN Referral_Date BETWEEN @StartDate AND @EndDate THEN 1 END) AS 'Referrals',
COUNT (CASE WHEN Att_Start_Dttm BETWEEN @StartDate AND @EndDate THEN 1 END ) AS 'Appointments'

FROM tbldiaryappointment DA
left outer join tblDiaryAppointmentClinicianInvitee CI on DA.Diary_Appointment_ID = CI.Diary_Appointment_ID
left outer join tblstaff ST on CI.Clinician_Invitee_Staff_ID = ST.STaff_ID
left outer join tbllocation LOC on LOC.Location_ID = DA.Scheduled_Location_ID
left outer join tblpatient PAT on PAT.Patient_ID = DA.Patient_ID
left outer join tblAttendanceTypeValues ATV on ATV.Attendance_Type_ID = DA.Attendance_Type_ID
left outer join tblDiaryAppointmentScheduledEvent SE on SE.Diary_Appointment_ID = DA.Diary_Appointment_ID
left outer join tblEventValues EVE on EVE.Event_ID = SE.Event_ID
left outer join tblProfessionalGroupValues PGV on ST.Professional_Group_ID = PGV.Professional_Group_ID
left outer join tblStaffJobRoleCodeValues JOB on JOB.Staff_Job_Role_Code_ID = ST.Job_Role_Code_ID
left outer join tblReferral REF on REF.Patient_ID = DA.Patient_ID
GROUP BY ST.Staff_ID