Logical order of execution(見「邏輯處理SELECT語句的順序「)以下SELECT
聲明:
DECLARE @MyTable TABLE
(
ID INT IDENTITY PRIMARY KEY,
[Date] SMALLDATETIME NOT NULL,
WorkingTime INT NOT NULL,
EmployeeID INT NULL
);
INSERT @MyTable ([Date], WorkingTime, EmployeeID)
SELECT '20130801', 1, 123 UNION ALL
SELECT '20130802', 0, 124 UNION ALL
SELECT '20130803', 0, 125;
SELECT x.ID,
CASE WHEN x.ID % 2 = 1 THEN x.ID END AS EmployeeID
FROM @MyTable x
WHERE EmployeeID IS NOT NULL;
是
- FROM @myTable X
- WHERE僱員IS NOT NULL(< => WHERE x.EmployeeID IS NOT NULL)
- SELECT x.ID,CASE ... END AS僱員
如您所見,CASE ... END AS EmployeeID
表達式評估爲AFTERWHERE
子句。這也意味着EmployeeID IS NOT NULL
謂詞引用x.EmployeeID
列而不是EmployeeID
別名從SELECT
子句。
這是我得到的結果如下理由:如果你想通過EmployeeID
別名來篩選,那麼你可以使用這些解決方案之一
ID EmployeeID
----------- -----------
1 1
2 NULL
3 3
:
SELECT x.ID,
CASE WHEN x.ID % 2 = 1 THEN x.ID END AS EmployeeID
FROM @MyTable x
WHERE CASE WHEN x.ID % 2 = 1 THEN x.ID END IS NOT NULL;
SELECT *
FROM
(
SELECT x.ID,
CASE WHEN x.ID % 2 = 1 THEN x.ID END AS EmployeeID
FROM @MyTable x
) src
WHERE src.EmployeeID IS NOT NULL;
注:這是使用與列名同名的別名不是一個好主意。這造成混亂。
邏輯問題。想要僅從我創建的列中返回非空值。謝謝 – user2800758