exists
(
select 1 from dt dt1 inner join pt pt1
on dt1.map=pt1.map and dt1.pda=pt1.pda
where c.code=dt1.customer and dt1.canceled=0
and dt1.doccode in ('02','03') and pt1.exp_value<>0
and dt1.insdate>='20151001'
and pt1.item in (
select item from f_itemset
where itemset='campaign78' and cat_custom='complexion_sccf')
)
and not exists
(
select 1 from dt dt2 inner join pt pt2
on dt2.map=pt2.map and dt2.pda=pt2.pda
where c.code=dt2.customer and dt2.canceled=0
and dt2.doccode in ('02','03') and pt2.exp_value<>0
and dt2.insdate>='20151001'
and pt2.item not in (
select item from f_itemset
where itemset='campaign78' and cat_custom='complexion_sccf')
)
这是在查询的
where
部分,表pt
和dt
也出现在from
子句中。
是否需要所有这些别名(
dt1
、dt2
等)?
如果我不给它们这些别名,它们会变成
dt
/pt
-与外部查询相关吗?
看看这个完美运行的(愚蠢的)查询:
SELECT * FROM TAGS AS T
WHERE T.[COUNT]< 10
AND T.ID IN (SELECT O.ID FROM TAGS AS O WHERE O.[COUNT]=9 OR O.ID = T.ID)
可以看到子查询使用了第一个的数据...
通常情况下,如果 SQL Server 可以确定要使用的女巫表,它会警告您,迫使您设置别名……但请记住,您应该始终输入别名。
因此您必须在查询中保留别名 pt1 和 pt2。