我创建了 2 个临时表#ClientsCountForDoctor
和#DeathCount
:
CREATE TABLE #ClientsCountForDoctor (DoctorCode int, Clients int)
SELECT
Personal.[DoctorCode], Count(Clients.[ClientCode]) AS [Count-Код клиента]
FROM
Personal
INNER JOIN
(Clients INNER JOIN MedicalCard ON Clients.[ClientCode] = MedicalCard.[ClientCode]) ON Personal.[DoctorCode] = MedicalCard.[DoctorCode]
GROUP BY
Personal.[DoctorCode];
INSERT INTO #ClientsCountForDoctor
EXEC nothing
CREATE TABLE #DeathCount (DoctorCode int, Deaths int)
SELECT
Personal.[DoctorCode], Count(Clients.[ClientCode]) AS [Count-Код клиента]
FROM
Personal
INNER JOIN
(Clients INNER JOIN MedicalCard ON Clients.[ClientCode] = MedicalCard.[ClientCode]) ON Personal.[DoctorCode] = MedicalCard.[DoctorCode]
GROUP BY
Personal.[DoctorCode], MedicalCard.[TreatmentResult]
HAVING
(((MedicalCard.[TreatmentResult])='Смерть'));
INSERT INTO #DeathCount
EXEC nothing
然后我想使用结果。我应该执行什么?
SELECT
Personal.[DoctorName],
#DeathCount.Deaths/#ClientsCountForDoctor.Clients AS [DeathPercent]
FROM
(#ClientsCountForDoctor INNER JOIN Personal ON #ClientsCountForDoctor.[DoctorCode] = Personal.[DoctorCode])
INNER JOIN
#DeathCount ON Personal.[DoctorCode] = #DeathCount.[DoctorCode];