我想在T-SQL中運行一個查詢來取回基於列爲空的數據集。IS忽略被忽略
這是代碼的簡化版本:
SELECT
T1.Col1, T1.Col2,
T1.Col3, T1.Col4
FROM
table1 AS T1
INNER JOIN
table2 AS T2 ON T1.Col2 = T2.Col3
WHERE
T2.Col4 IS NULL
問題是,所述結果包括其中T2.Col4是NULL,也不是NULL的行,這就像WHERE子句不存在。
任何想法將大大
更新 - 代碼完整版:
SELECT
M.ref
,C.cname
,CL.clname
,C.ccity
,M.productLine
,M.code
,CL.date
,M.dept
,DPT.group
,TK2.tkname
,TK2.tkdept
FROM DB.dbo.manage AS M
OUTER JOIN DB.dbo.ClientManageRelationship AS CMR
ON CMR.RelatedEntityID = M.EntityID
OUTER JOIN DB.dbo.Client AS C
ON C.EntityID = CMR.EntityID
INNER JOIN DB.dbo.ManageCustomerRelationship AS MCR
ON MCR.EntityID = M.EntityID
INNER JOIN DB.dbo.Customer AS CL
ON CL.EntityID = MCR.RelatedID
INNER JOIN DB.dbo.timek AS TK
ON TK.tki = M.tkid
LEFT JOIN (SELECT Group = division, [Department] = newdesc, deptcode FROM DB.csrt.vw_rep_p_l_dept) AS DPT
ON tkdept = DPT.dept
LEFT JOIN (SELECT Name = TK2.tkfirst + ' ' + TK2.tklast, TK2.tki, TK2.dept, TK2.loc FROM DB.dbo.timek as TK2 WITH(NOLOCK)) AS TK2
ON TK2.tki = M.tkid
WHERE DPT.Department = 'Casualty'
AND UPPER (C.ClientName) LIKE '%LIMITED%'
AND CL.date > '31/12/2014'
AND CL.Date IS NULL
AND TK.tkloc = 'loc1' OR TK.tkloc = 'loc2'
ORDER BY M.ref
你確定嗎?可以將T2.Col4添加到您的選擇和發佈樣本數據和輸出(以文本表格格式)? – etsa
當你運行沒有where的查詢時,你會得到NULLS嗎? –
對不起,我應該更清楚。 T2Col4也被選中,所以我可以看到結果。我已經取出where子句,我得到了NULLS和NOT NULLS,但是我得到了一個沒有WHERE子句的更大的結果集。此外,還有更多的連接比我的示例代碼中,我不知道這是否有所作爲? – mark