2016-03-07 92 views
0

我正在做一個查詢,但並不是所有的用戶名只顯示與值。請檢查我的代碼,看看我的where子句中腳麻SQL沒有返回值,它是NULL

Select a.Owner_Id, 
    a.Creator_Id, 
    b.Name as KPI, 
    B.Record_Type, 
    B.Resource_Id, 
    C.Display_Name, 
    Count(a.owner_id) AS MAX, 
    case when a.owner_Id is null then c.Display_name else 0 end 
from amgr_appointments_tbl A 
    left join AMGR_Resources B on A.Owner_Id = b.Resource_Id 
    left join ADMN_User_Details AS C ON A.Creator_Id = C.User_Id 
where b.Resource_Id in('ROE534B758E', 'R0E42A431B5', 'R0E42A4BB3F','R0E42A3E514','R0E42A44D19', 'R0E42A37FBB') 
    and b.Record_Type = 3 
    and a.Creator_Id In('AMCKENZIE','ASARAK','CWEIMANN', 'EWOOLDRIDGE', 'GHAVENGA', 'JSTAPELBERG','WILLEMB', 'YRHODA', 'JMALAN') 
    and A.App_Date BETWEEN '20160201' and '20160331' 
Group by a.Owner_Id, a.Creator_Id, b.Name, B.Record_Type, B.Resource_Id, c.Display_Name 
+0

抱歉搞砸了這個問題 – Stix

+0

您的查詢以'amgr_appointments_tbl'開頭。對於這個tbl,你可以用'LEFT JOIN'連接另外兩個表。這將顯示約會沒有用戶,但沒有約會的用戶...如果你想看到所有用戶,從那裏開始,或嘗試'RIGHT JOIN' – Shnugo

回答

0

你的條件對bleft join爲內連接。你需要那些條件移到on條款:

select . . . 
from amgr_appointments_tbl A left join 
    AMGR_Resources B 
    on A.Owner_Id = b.Resource_Id and 
     b.Resource_Id in ('ROE534B758E', 'R0E42A431B5', 'R0E42A4BB3F','R0E42A3E514','R0E42A44D19', 'R0E42A37FBB') and 
     b.Record_Type = 3 left join 
    ADMN_User_Details C 
    on A.Creator_Id = C.User_Id 
where . . . 

第一表的條件確實屬於該where子句。

+0

謝謝,解決了我的問題 – Stix

+0

新的查詢返回的一切,我希望它返回的所有用戶都是在Creater_Id中指定的用戶 – Stix

+0

「a」表中的條件應該放在where子句中,而不是on子句中。 –