2013-01-25 34 views
1

为什么这个简单的查询工作正常在Oracle中,但DB2不携手在ON子句:为什么子查询不DB2

select * 
from 
sysibm.dual d1 
left join sysibm.dual d2 on 1=1 and exists (select 1 from sysibm.dual) 

移动子查询,包括条件,where子句可以帮助,但将限制外部连接到内部。

回答

1

当我尝试运行你的查询,我得到一个-338 error,而根据信息中心(见链接),也有对ON条款以下限制:关联

ON子句JOIN运算符或MERGE语句 由于以下某个原因而无效。

* The ON clause cannot include any subqueries. 
* Column references in an ON clause must only reference columns 
    of tables that are in the scope of the ON clause. 
* Scalar fullselects are not allowed in the expressions of an ON clause. 
* A function referenced in an ON clause of a full outer join 
    must be deterministic and have no external action. 
* A dereference operation (->) cannot be used. 
* A SQL function or SQL method cannot be used. 
* The ON clause cannot include an XMLQUERY or XMLEXISTS expression. 

我不知道是否有可能与您的查询,但你认为也许你可以重新写这样的事:

select * 
from 
sysibm.dual d1 
left join (
    SELECT dl.*, 
    CASE WHEN EXISTS (SELECT 1 FROM sysibm.dual) 
     THEN 1 
     ELSE 0 
    END AS jn 
    FROM sysibm.dual dl 
) D2 
    on 1=1 and 1=d2.jn 
+0

我可以重写它,但正如我认为加入子查询一样,因为表格可能会导致全面扫描。我有抽象例子:'1)SELECT * FROM 占一个 连接操作o在(o.acc_id = a.acc_id 和存在(从操作 选择空 其中ACC_ID = o.acc_id 具有最大值(ope_date)= o.ope_date)) 2)SELECT * FROM 占一个 连接操作o在(o.acc_id = a.acc_id) 加入(选择最大值(ope_date)作为ope_date,ACC_ID从操作 组由ACC_ID)latestope on(latestope.acc_id = o.acc_id and latestope.ope_date = o.ope_date)'第二个引起全面扫描,因为我认为 – user1741227

+0

第二个查询可以像'SELECT * FROM占一个 JOIN操作ö ON o.acc_id = a.acc_id WHERE o.ope_date =( \t SELECT MAX(ope_date) \t FROM操作ð \t WHERE o.acc_id = d.acc_id )' 如果您的索引包含'o.acc_id'和'o.ope_date',那么DB2将使用索引来解析MAX()。我们在'$ work'有一个类似的表,并且类似的查询运行速度非常快。 – bhamby

0

这个工作在DB2 V10.1! 没有安装修复包。