这是我的CTE / SQL声明:
with cte as (
...
)
select 1, 0, q.quadrantID, 0, '', 1
from Quadrants q
where q.quadrantID not in (select cte.quadrantID from cte)
order by quadrantID ASC
象限表有6条记录。
此方案中的CTE不会返回任何记录。我的选择应该从象限中返回6条记录,但事实并非如此。那是为什么?
如果我运行以下声明:
select 1, 0, q.quadrantID, 0, '', 1
from Quadrants q
where q.quadrantID not in (5)
order by quadrantID ASC
它将返回6行中的5行,就像它应该的那样。
答案 0 :(得分:5)
如果您的CTE在数据中返回NULL,您是否知道NOT IN
将返回不正确的结果。
尝试运行此查询并查看结果,您将获得零记录
select 1, 0, q.quadrantID, 0, '', 1
from Quadrants q
where q.quadrantID not in (NULL,5)
order by quadrantID ASC
您应该使用NOT EXISTS
编写查询,以获取更多信息。
http://decipherinfosys.wordpress.com/2007/01/21/32/
修改我用NOT EXISTS
子句编写了您的查询,试试这个
select 1, 0, q.quadrantID, 0, '', 1
from Quadrants q
where NOT EXISTS
(
SELECT 1 from cte WHERE cte.quadrantID=q.quadrantID
)
order by quadrantID ASC
答案 1 :(得分:0)
; WITH cte AS (
...
)
SELECT 1, 0, quadrantID, 0, '', 1
FROM
( SELECT quadrantID
FROM Quadrants q
EXCEPT
SELECT quadrantID
FROM cte
) tmp
ORDER BY quadrantID ASC ;