<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Using a query result in another query in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777375#M1042</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am a GIS analyst and very new to Python API. I hope to get some advice on creating syntax.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is my scenario.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. get the record count in feature layer. this will be used to grab the record with max object id. (the very last object id will be matched with row count always).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #339966;"&gt;&lt;EM&gt;query_result1 = workingLayer.query(return_count_only=True)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #339966;"&gt;&lt;EM&gt;query_result1&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE style="color: #000000; background-color: #ffffff; border: 0px; font-size: 14px; padding: 1px 0px;"&gt;&lt;SPAN style="color: #339966;"&gt;&lt;EM&gt;49&lt;/EM&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;2. &lt;SPAN style="text-decoration: underline;"&gt;Using the result from #1 in another query.&lt;/SPAN&gt; 'where' part is where I am having an issue with. I am not sure how I can use the result from #1 with the where syntax.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;query_result2 = workingLayer.query(&lt;SPAN style="color: #ff0000;"&gt;where = ,&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;out_fields='Region, Date')&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried with what I used to with arcpy...something like this. But not working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;SPAN style="color: #3366ff;"&gt;whereClause&lt;/SPAN&gt; = "'ObjectId=" + str(query_result1) + "'"&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;query_result2 = workingLayer.query(&lt;SPAN style="color: #ff0000;"&gt;where = &lt;SPAN style="color: #3366ff;"&gt;whereClause&lt;/SPAN&gt;,&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;out_fields='Region, Date')&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;K&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 23 Apr 2020 04:46:09 GMT</pubDate>
    <dc:creator>KerryKang</dc:creator>
    <dc:date>2020-04-23T04:46:09Z</dc:date>
    <item>
      <title>Using a query result in another query</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777375#M1042</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am a GIS analyst and very new to Python API. I hope to get some advice on creating syntax.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is my scenario.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. get the record count in feature layer. this will be used to grab the record with max object id. (the very last object id will be matched with row count always).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #339966;"&gt;&lt;EM&gt;query_result1 = workingLayer.query(return_count_only=True)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #339966;"&gt;&lt;EM&gt;query_result1&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE style="color: #000000; background-color: #ffffff; border: 0px; font-size: 14px; padding: 1px 0px;"&gt;&lt;SPAN style="color: #339966;"&gt;&lt;EM&gt;49&lt;/EM&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;2. &lt;SPAN style="text-decoration: underline;"&gt;Using the result from #1 in another query.&lt;/SPAN&gt; 'where' part is where I am having an issue with. I am not sure how I can use the result from #1 with the where syntax.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;query_result2 = workingLayer.query(&lt;SPAN style="color: #ff0000;"&gt;where = ,&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;out_fields='Region, Date')&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried with what I used to with arcpy...something like this. But not working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;SPAN style="color: #3366ff;"&gt;whereClause&lt;/SPAN&gt; = "'ObjectId=" + str(query_result1) + "'"&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;query_result2 = workingLayer.query(&lt;SPAN style="color: #ff0000;"&gt;where = &lt;SPAN style="color: #3366ff;"&gt;whereClause&lt;/SPAN&gt;,&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;out_fields='Region, Date')&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;K&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Apr 2020 04:46:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777375#M1042</guid>
      <dc:creator>KerryKang</dc:creator>
      <dc:date>2020-04-23T04:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using a query result in another query</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777376#M1043</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="background-color: #ffffff; border: 0px;"&gt;From:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;SPAN style="border: 0px; font-weight: inherit;"&gt;&lt;EM&gt;&lt;SPAN style="color: #3366ff; border: 0px; font-weight: inherit;"&gt;whereClause&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;= "'ObjectId=" + str(query_result1) + "'"&lt;/EM&gt;&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;This looks to me like your resulting where clause would be&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;'ObjectId=49'&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Try it without the single quotes:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro" style="background: none #f6f6f6; border-left: 2px solid #cccccc; margin: 10px 0px; padding: 10px 20px;"&gt;&lt;SPAN style="border: 0px; font-weight: inherit;"&gt;&lt;EM&gt;&lt;SPAN style="color: #3366ff; border: 0px; font-weight: inherit;"&gt;whereClause&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;= "ObjectId=" + str(query_result1)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;In general I prefer the string format so I can better see the final string, but the result should be the same:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;whereClause = "ObjectId = {0}".format(query_result1)&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Also depending on your database management system, the case of "ObjectId" ("OBJECTID") might matter.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit in case anyone else sees this and wonders why I suggested removing the single quotes: I made an assumption that the query function is building a database query by inserting that parameter. A basic query with a WHERE clause might be, "SELECT * FROM working_table WHERE ObjectId = 1". By putting that parameter in single quotes, the query language treats it as a string: "... WHERE 'ObjectId=49'", so it is looking to compare that to a text field in the database. The query language would expect "WHERE 'ObjectId=49' = some_text_field".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Different query languages might return all results, return no results, or return an error. That I'm not sure about.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Apr 2020 15:29:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777376#M1043</guid>
      <dc:creator>WillHouston</dc:creator>
      <dc:date>2020-04-24T15:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using a query result in another query</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777377#M1044</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It worked, thanks Will!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;K.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Apr 2020 21:55:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777377#M1044</guid>
      <dc:creator>KerryKang</dc:creator>
      <dc:date>2020-04-27T21:55:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using a query result in another query</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777378#M1045</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If someone's response answers your question, please mark it correct to close out the question.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Apr 2020 13:57:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/using-a-query-result-in-another-query/m-p/777378#M1045</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-04-28T13:57:21Z</dc:date>
    </item>
  </channel>
</rss>

