2017-06-06 112 views
0

我知道我已經完成了這一點,現在我想不出解決方案了......我知道這是某種完全外部連接。返回不同的匹配和不匹配的行(外部連接?)

我有3個表:

ACTIVITIES  
id name   
---+------------- 
1 | Basketball 
2 | Chess Club 

ENROLLMENT     
id activity_id person_id 
---+-------------+----------- 
1 | 1   | 1   
2 | 1   | 2   
3 | 2   | 1   

ATTENDANCE 
id date   person_id activity_id 
---+-------------+----------+--------------- 
1 | 2017-01-01 | 1  | 1 
2 | 2017-01-01 | 2  | 1 
3 | 2017-01-02 | 1  | 1 

我試圖通過person_id拿到出勤,甚至當該ID沒有一個日期存在:

date   person_id 
------------+--------------- 
2017-01-01 | 1 
2017-01-02 | null 

這裏的東西接近到我認爲我需要的...

select date, attendance.person_id 
from enrollment 
**SOME SORT OF JOIN** attendance on enrollment.person_id = attendance.person_id 
where person_id = 1 

但是我能得到的所有回報是:

date   person_id 
------------+--------------- 
2017-01-01 | 1 
2017-01-01 | 1 

...其中行數是正確的,但值是錯誤的。

+0

@耶爾馬茲,因爲它會與招生,其中'person_id'存在被連接,並通過'activity_id'我的僞查詢並不完全證明它 – Grant

回答

1

這似乎導致你想要的結果:

select date, 
     max(case when person_id = 1 then person_id end) as person_id 
from attendance a 
group by date 
order by date; 
+0

我的英雄......謝謝你 – Grant