我有以下查询我在mResourceAllocation
和mResource
表中使用外键的所有选定列。在mResourceAllocation
外键列中,CoreFunction_ID(Function)和Product_ID(Product)都可以为空。我使用INNER JOINS
从其他表中获取值。我想要的是获得输入@businessUnitId和@reportDate的所有记录,包括CoreFunction_ID列和Product_ID列中的空条目。请帮忙。提前谢谢。
SELECT Distinct
[Employee ID] = r.Resource_Employee_ID,
[Resource ID] = r.Resource_ID,
[Resource Name] = r.Resource_First_Name+' '+r.Resource_Last_Name,
[Country] = c.Country_Name,
[Location] = l.Location_Short_Name,
[BU] = bu.BusinessUnit_Name,
[Function] = cf.Core_Function_Name,
[Product] = ISNULL (p.Product_Name,'Unknown'),
[Assignment Start Date] = ra.Resource_Allocation_From_Date,
[Assignment End Date] = ra.Resource_Until_Date,
[Time Allocated] = ra.Resource_Allocation_Percentage,
[Training] = ra.Resource_Training,
[Admin] = ra.Resource_Admin,
[Roadmap] = ra.Resource_Roadmap,
[Defect] = ra.Resource_Defect,
[Commission] = ra.Resource_Commission,
[Effectiveness] = ra.Resource_Efficiency_Percentage
INTO #ResourceAllocated
FROM mResource r
INNER JOIN mResource ON r.IsDeleted = 0
INNER JOIN mResourceAllocation ra ON ra.IsDeleted = 0 AND ra.BusinessUnit_ID = @businessUnitId AND r.Resource_ID = ra.Resource_ID AND @reportDate >= ra.Resource_Allocation_From_Date AND (@reportDate <= ra.Resource_Until_Date OR ra.Resource_Until_Date IS NULL)
INNER JOIN mBusinessUnit bu ON bu.IsDeleted = 0 AND bu.BusinessUnit_ID = @businessUnitId
INNER JOIN mCountry c ON r.Resource_Country_ID = c.Country_ID
INNER JOIN mLocation l ON r.Resource_Location_ID = l.Location_ID
INNER JOIN mCoreFunction cf ON ra.CoreFunction_ID = cf.Core_Function_ID
INNER JOIN mProduct p ON p.IsDeleted = 0 AND ra.Product_ID = p.Product_ID
答案 0 :(得分:0)
而不是
INNER JOIN mCoreFunction cf ON ra.CoreFunction_ID = cf.Core_Function_ID
INNER JOIN mProduct p ON p.IsDeleted = 0 AND ra.Product_ID = p.Product_ID
使用
LEFT JOIN mCoreFunction cf ON ra.CoreFunction_ID = cf.Core_Function_ID
LEFT JOIN mProduct p ON p.IsDeleted = 0 AND ra.Product_ID = p.Product_ID