SQL LEFT JOIN在MS Access中不起作用

时间:2012-08-08 17:21:51

标签: sql ms-access left-join

以下 SQL查询不会为MS Access 中的 LEFT JOIN返回任何数据。

SELECT * FROM 
 (
 SELECT Operation_Part.PPC,
        Operation_Part.TargetOperationsPerHour as JPH,
        Operation_Part.Misc1 as [JPh Alt 1],
        STR(Operation_Part.SeqNr) as Sequence,
        Operation_Part.idPart,
        Operation_Part.idOperationPart, 
        Operation.OperationType as Operation,
        tblOperationType.OperationType as [Operation Type] 
 FROM tblOperationType 
   RIGHT JOIN (Operation INNER JOIN Operation_Part ON Operation.idOperation = Operation_Part.idOperation)
    ON tblOperationType.idOpType = Operation.OperationTID  
 WHERE Operation_Part.VsbLDq = 0
   AND Operation_Part.idPart <> 0  AND Operation_Part.idPart = 1271) 
 AS [AA]

 LEFT JOIN (SELECT Sum([Cptotal]) AS DownTime, 
            TransactionDetail.idPart, 
            STR(TransactionDetail.seq_number) as Sequence  
            FROM ([Transaction] INNER JOIN TransactionDetail ON [Transaction].idTransaction = TransactionDetail.idTransaction)  
           WHERE [Transaction].idTransactionType=29 
             AND TransactionDetail.WorkOrderNumber = 'PR23144'  
           GROUP BY TransactionDetail.idPart, STR(TransactionDetail.seq_number)) 
 AS [EE] 
 ON AA.idPart = EE.idPart AND EE.Sequence=AA.Sequence

在SQL Server中,查询会根据需要返回1.08的停机时间值(请参阅下面的图片)。

首先选择返回:

enter image description here

第二次选择返回:

enter image description here

MS Access结果:

enter image description here

SQL服务器结果:

enter image description here


如何在MS Access中使其正常工作?

1 个答案:

答案 0 :(得分:1)

这只是猜测,但它可能与您不想要的行的适用列中的空值有关。

建议你改变

     SELECT Sum([Cptotal]) AS DownTime,

     SELECT Sum(IIf(IsNull([CpTotal]), 0, [CpTotal])) AS DownTime

在Access中我总是使用CStr(...)而不是Str(...)

除此之外,尽管可能很痛苦,但我建议将左连接组件转换为单独的查询,或者如果您不使用查询,则使用此数据构建临时表,然后将其连接到原始数据库中查询。