How to debug definition queries in ArcMap?

Troubleshooting and debugging ArcMap defintion queries that go beyond a basic single statement can be very frustrating. Statements that work on their own easily break when combined. How can this pain be alleviated?

For example say we want to select all records of last 3 years, and then from that set only the month of march.

In a file-gdb this query selects all records 3 years old and newer:

EXTRACT(YEAR FROM "Date") >= (EXTRACT(YEAR FROM CURRENT_DATE) - 2)

While this query selects the month of March:

EXTRACT(MONTH FROM "Date") = 3

However combining them by inserting year_query in place of 'Date' in the month query will fail with "an invalid SQL statement was used". (On multiple lines for readability, it's all in a single line in query builder):

EXTRACT(MONTH FROM 
(EXTRACT(YEAR FROM "Date") >= (EXTRACT(YEAR FROM CURRENT_DATE) - 2))
) = 3

For the example the solution is to use AND instead of nesting:

(EXTRACT(YEAR FROM "Date") >= (EXTRACT(YEAR FROM CURRENT_DATE) - 2))
AND (EXTRACT(MONTH FROM Date ) = 3)

…but how to get this answer when you don't know it already?

One solution:

Stack" your definition queries in different layers.

Save each successful query as a separate layer, then use them as input into your next query. On saving the new feature layer ArcMap will combine the queries into a single one that works.

Last 3 years query layer:

clip_image001

March from all years query layer:

clip_image002

With Make Feature Layer tool, use "Last 3 years" as input layer and put the "March all years" query into the expression field:

clip_image003

Result, a working "Only March from last 3 years" layer:

clip_image004

From <http://gis.stackexchange.com/questions/231140/how-to-debug-definition-queries-in-arcmap/231141#231141>

Leave a Reply

Your email address will not be published. Required fields are marked *