[AccessD] Add criteria to query

Stuart McLachlan stuart at lexacorp.com.pg
Wed Oct 9 16:48:39 CDT 2019


>  I then added criteria for Sym Code of  "infill". The issue is all records
of Sym Code "infill" are being returned. Your assistance appreciated.

If you split your SQL into lines and ident on brackets it makes it much easier to identify the 
problems caused by incorrect bracketing of complex conditions:

The "infill" condition is only being applied to the Last_Status condition inside the outer "OR"
If the first half of the condition is met, the "infill" condition is not tested.


HAVING 
(
	(
		([tbl Petra Current Status Copy].[Sym Code]) Not Like "PA*" 
		And 
		([tbl Petra Current Status Copy].[Sym Code])<>"P&A"
		And 
		([tbl Petra Current Status Copy].[Sym Code])<>"side"
	)
)

OR 

(
	(
		(dbo_DSS_LastStatus.Status)="TA" 
		Or
		(dbo_DSS_LastStatus.Status)="P&A"
	) 
		AND 
		(
			([tbl Petra Current Status Copy].[Sym Code])="infill"
		)
);





On 9 Oct 2019 at 14:05, Kaup, Chester wrote:

> SELECT dbo_DSS_LastStatus.PID, dbo_DSS_LastStatus.Completion_Name,
> dbo_DSS_LastStatus.Status, [tbl Petra Current Status Copy].[Sym Code]
> INTO [tbl Petra and DSS Status] FROM (dbo_DSS_LastStatus INNER JOIN
> dbo_DSS_CompletionMaster ON dbo_DSS_LastStatus.PID =
> dbo_DSS_CompletionMaster.PID) INNER JOIN [tbl Petra Current Status
> Copy] ON dbo_DSS_CompletionMaster.ParentPID = [tbl Petra Current
> Status Copy].[UWI (APINum)] GROUP BY dbo_DSS_LastStatus.PID,
> dbo_DSS_LastStatus.Completion_Name, dbo_DSS_LastStatus.Status, [tbl
> Petra Current Status Copy].[Sym Code] HAVING ((([tbl Petra Current
> Status Copy].[Sym Code]) Not Like "PA*" And ([tbl Petra Current Status
> Copy].[Sym Code])<>"P&A" And ([tbl Petra Current Status Copy].[Sym
> Code])<>"side")) OR (((dbo_DSS_LastStatus.Status)="TA" Or
> (dbo_DSS_LastStatus.Status)="P&A") AND (([tbl Petra Current Status
> Copy].[Sym Code])="infill"));




More information about the AccessD mailing list