2015-10-04 89 views
1

我有兩張表,一個照顧者和MS SQL Server中的另一個患者。每個病人被分配了一個數字老者的SQL加入和計數

╔════════════════╦══════════════════╦════════════════════╦═════════════════╗ 
║ Patient Name ║ Primary Carer ║ Secondary Carer ║ Tertiary Carer ║ 
╠════════════════╬══════════════════╬════════════════════╬═════════════════╣ 
║ Joe Bloggs  ║ John Smith  ║ Oscar Wild  ║ Tom Cruise  ║ 
║ Mary Sue  ║ John Smith  ║ Matt Smith  ║ Peter Pan  ║ 
║ Peter Parker ║ John Smith  ║ Oscar Wild  ║ Matt Smith  ║ 
╚════════════════╩══════════════════╩════════════════════╩═════════════════╝ 

我期待選擇我的照顧者表中的所有記錄,然後計算該護工有多少次是主,在患者表二級或三級照顧者包括那些護理人員誰沒有分配給任何人。

例如;

╔═══════════════╦═══════════════════════╦═════════════════════════╦═══════════════════╗ 
║ Carer   ║ Primary Carer Freq ║ Secondary Carer Freq ║ Third Carer Freq ║ 
╠═══════════════╬═══════════════════════╬═════════════════════════╬═══════════════════╣ 
║ John Smith ║     3 ║      0 ║     0 ║ 
║ Oscar Wilde ║     0 ║      2 ║     0 ║ 
║ Tom Cruise ║     0 ║      0 ║     1 ║ 
║ Matt Smith ║     0 ║      1 ║     0 ║ 
║ Peter Pan  ║     0 ║      0 ║     1 ║ 
║ Barry White ║     0 ║      0 ║     0 ║ 
╚═══════════════╩═══════════════════════╩═════════════════════════╩═══════════════════╝ 

感謝您的幫助!

回答

3

您可以使用:

SqlFiddleDemo

SELECT 
    [name] 
    ,[Primary Carer Freq] = COUNT(p1.[Primary Carer]) 
    ,[Secondary Carer Freq] = COUNT(p2.[Secondary Carer]) 
    ,[Third Carer Freq]  = COUNT(p3.[Tertiary Carer]) 
FROM carers c 
LEFT JOIN patients p1 
    ON p1.[Primary Carer] = c.[Name] 
LEFT JOIN patients p2 
    ON p2.[Secondary Carer] = c.[Name] 
LEFT JOIN patients p3 
    ON p3.[Tertiary Carer] = c.[Name] 
GROUP BY [name]; 

數據:

CREATE TABLE patients([Patient Name] VARCHAR(100), 
         [Primary Carer] VARCHAR(100), 
         [Secondary Carer] VARCHAR(100), 
         [Tertiary Carer] VARCHAR(100)); 


INSERT INTO patients 
SELECT 'Joe Bloggs',  'John Smith', 'Oscar Wild', 'Tom Cruise' 
UNION ALL SELECT 'Mary Sue', 'John Smith',  'Matt Smith', 'Peter Pan' 
UNION ALL SELECT ' Peter Parker', 'John Smith',  'Oscar Wild', 'Matt Smith'; 

CREATE TABLE carers(Name VARCHAR(100)); 

INSERT INTO carers(Name) 
SELECT 'John Smith' 
UNION ALL SELECT 'Oscar Wilde' 
UNION ALL SELECT 'Tom Cruise' 
UNION ALL SELECT 'Matt Smith' 
UNION ALL SELECT 'Peter Pan' 
UNION ALL SELECT 'Barry White'; 

編輯:

如果你想綜合作用的結果使用+

SELECT 
    [name] 
    ,[Primary&Secondary Carer Freq] = COUNT(p1.[Primary Carer]) + COUNT(p2.[Secondary Carer]) 
    ,[Third Carer Freq]    = COUNT(p3.[Tertiary Carer]) 
+0

一個子查詢中選擇是最無效率的查詢中的一個,在由外部選擇返回的每一行執行的每個子查詢中選擇。 –

+0

@ M.Ali同意我準備版本'trif'LEFT JOIN'。稍等片刻 – lad2025

+0

如果先三次執行「GROUP BY」,然後再執行三次「LEFT JOIN」,則效率可能會更高。無論如何,+1,因爲它返回正確的結果。 –

1

SQL Fiddle

MS SQL Server 2008的架構設置

CREATE TABLE Test_Table(PatientName VARCHAR(20), PrimaryCarer VARCHAR(20), 
         SecondaryCarer VARCHAR(20),TertiaryCarer VARCHAR(20)); 

INSERT INTO Test_Table VALUES      
    ('Joe Bloggs' ,  'John Smith' ,  'Oscar Wild'  ,  'Tom Cruise'),  
    ('Mary Sue'  ,  'John Smith' ,  'Matt Smith'  ,  'Peter Pan'),  
    ('Peter Parker' ,  'John Smith' ,  'Oscar Wild'  ,  'Matt Smith'); 

查詢1

SELECT CarerName 
     ,COUNT(CASE WHEN CarerType = 'PrimaryCarer' THEN 1 END) AS PrimaryCarer 
     ,COUNT(CASE WHEN CarerType = 'SecondaryCarer' THEN 1 END) AS SecondaryCarer 
     ,COUNT(CASE WHEN CarerType = 'TertiaryCarer' THEN 1 END) AS TertiaryCarer 
FROM Test_Table T 
UNPIVOT (CarerName FOR CarerType IN(PrimaryCarer,SecondaryCarer,TertiaryCarer) 
     )up 
GROUP BY CarerName 

Results

| CarerName | PrimaryCarer | SecondaryCarer | TertiaryCarer | 
|------------|--------------|----------------|---------------| 
| John Smith |   3 |    0 |    0 | 
| Matt Smith |   0 |    1 |    1 | 
| Oscar Wild |   0 |    2 |    0 | 
| Peter Pan |   0 |    0 |    1 | 
| Tom Cruise |   0 |    0 |    1 | 
+0

我想你錯過了'巴里白色',即'左加入與護理員表。仍然+1,因爲這個變種只掃描一次患者表,而不是三次。 –