0

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];
4

1 に答える 1

2

Select Into必要なのは構文だと思います。It will create the table for you with the selected data inserted at the same time. 次に、最後のクエリから、作成されたテーブルを使用して通常の選択を行います

元:

SELECT col1,col2,..
INTO #temp          --Note: this temporary table will be created in the db
FROM yourTable

クエリ:

SELECT Personal.[DoctorCode], 
       Count(Clients.[ClientCode]) AS [Count-Код клиента]
INTO #ClientsCountForDoctor --NOTE

FROM Personal INNER JOIN 
 (Clients INNER JOIN MedicalCard ON Clients.[ClientCode] = MedicalCard.[ClientCode]) 
           ON  Personal.[DoctorCode] = MedicalCard.[DoctorCode]
GROUP BY Personal.[DoctorCode];


SELECT Personal.[DoctorCode], 
       Count(Clients.[ClientCode]) AS [Count-Код клиента]
#DeathCount  --NOTE

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])='Смерть'));


SELECT p.[DoctorName], d.Deaths/c.Clients AS [DeathPercent]
FROM #ClientsCountForDoctor c INNER JOIN Personal p
     ON c.[DoctorCode] = p.[DoctorCode]
           INNER JOIN #DeathCount d 
     ON p.[DoctorCode] = d.[DoctorCode];
于 2012-12-21T10:50:35.797 に答える