简体   繁体   中英

SQL LEFT JOIN not working in MS Access

The following SQL query returns no data for the LEFT JOIN in MS Access .

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

In SQL Server the query does return the downtime value of 1.08 as required (see pics below).

First select returns:

在此处输入图片说明

Second select returns:

在此处输入图片说明

MS Access result:

在此处输入图片说明

SQL server result:

在此处输入图片说明


How do I make it work in MS Access?

This is only a guess, but it may well have something to do with the nulls in the applicable columns of the rows you dont really want.

Suggest you change

     SELECT Sum([Cptotal]) AS DownTime,

to

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

In Access I always use CStr(...) instead of Str(...)

Aside from this, painful though it may be, I'd suggest turning the left-joined component into a separate query, or if you dont use queries, building a temporary table with this data which is then left joined into the original query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM