SQL不返回值为NULL的值

时间:2016-03-07 12:51:42

标签: sql

我正在进行查询,但并非所有User_names都只显示具有值的查询。请检查我的代码并查看我出错的地方

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

1 个答案:

答案 0 :(得分:0)

b子句中where的条件会将left 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 . . .

first 表中的条件属于where子句。