2016-03-02 69 views
0

我想拉0值,但是我的查詢不允許0值,並且需要很長時間才能執行。返回0每個用戶的值

這裏是我的代碼:

select a.Owner_Id, 
b.Name as KPI, 
B.Record_Type, 
B.Resource_Id, 
C.Display_Name, 
count(*) as Max 

from amgr_appointments A 
right outer join AMGR_Resources B on A.Owner_Id = b.Resource_Id 
right outer 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', 'JMALAN') 
and A.App_Date between '2016-02-29'and '2016-03-29' 
Group by a.Owner_Id, a.Creator_Id, b.Name, B.Record_Type, B.Resource_Id, c.Display_Name 

我得到如下結果:

amckenzie 1 Pop-in 
ASARAK 2 Pop-in 

不過,我想這樣的結果,包括即使該值是0

amckenzie 1 Pop-in 
ASARAK 2 Pop-in 
JMALAN 0 Pop-in 
用戶

請幫助我,告訴我哪裏會出錯。我曾嘗試不同的連接然而在查詢運行時間過長

+2

的樣本數據將真正幫助,對此你得到這個輸出。另外提及你將使用的DB /版本 – Utsav

+0

我想你想要顯示的數量(*)可以是0.如果是這樣,你可以嘗試IFNULL(count(*),0)。 – Chuck

+0

IFNULL(count(),0)。對於所有行顯示0,如果用戶沒有約會,我想要返回值0 – Stix

回答

0

嘗試下面的所有表

SELECT a.Owner_Id, 
      b.Name as KPI, 
      B.Record_Type, 
      B.Resource_Id, 
      C.Display_Name, 
      count(*) as Max 
    FROM amgr_appointments A 
    RIGHT OUTER JOIN AMGR_Resources B 
     ON (A.Owner_Id = b.Resource_Id 
     AND a.Creator_Id In('AMCKENZIE','ASARAK', 'JMALAN') 
     AND A.App_Date BETWEEN '2016-02-29'AND '2016-03-29') 
    RIGHT OUTER 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 
    GROUP BY a.Owner_Id, a.Creator_Id, b.Name, B.Record_Type, B.Resource_Id, c.Display_Name