In the service there is field name "sysDate" with Date type.
Is there any way to select the record with minimum sysDate?
Hello Randy,
I check the query object in ArcGIS Js API and get the idea:
query.orderByFields=["SystemDate "];
query.num=1; //Number of features to retrieve.
Then the queryTask will return the feature with minimum SystemDate.
Thank you so much for your help.
Have a nice day.
Here's my python code to query the REST API. In my case, I was querying for a survey date, so substitute it for "sysDate". In the query dictionary, the where clause should insure that the date field is not null, but it could be adjusted to search for a date after/before a specific date. Limit the output to 1 row with "resultRecordCount".
For additional help, see: Query (Feature Service/Layer)
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> urllib<SPAN class="punctuation token">,</SPAN> urllib2<SPAN class="punctuation token">,</SPAN> json<SPAN class="punctuation token">,</SPAN> sys<SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> collections <SPAN class="keyword token">from</SPAN> datetime <SPAN class="keyword token">import</SPAN> datetime<SPAN class="punctuation token">,</SPAN> timedelta <SPAN class="comment token"># URL, referrer and tokenurl may vary based on ArcGIS Online or your server setup</SPAN> <SPAN class="comment token"># Credentials and feature service information</SPAN> username <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># user name here</SPAN> password <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># password here</SPAN> <SPAN class="comment token"># Adjust URL as required</SPAN> URL <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fservices.arcgis.com%2F" target="_blank">https://services.arcgis.com/</A><SPAN><xyz>/arcgis/rest/services/<layername>/FeatureServer/0/query"</SPAN></SPAN> referer <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fwww.arcgis.com%2F" target="_blank">http://www.arcgis.com/</A><SPAN>"</SPAN></SPAN> tokenurl <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fwww.arcgis.com%2Fsharing%2Frest%2FgenerateToken" target="_blank">https://www.arcgis.com/sharing/rest/generateToken</A><SPAN>"</SPAN></SPAN> <SPAN class="comment token"># obtain a token</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">:</SPAN> username<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">:</SPAN> password<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'referer'</SPAN><SPAN class="punctuation token">:</SPAN> referer <SPAN class="punctuation token">}</SPAN> query_string <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>urlencode<SPAN class="punctuation token">(</SPAN>query_dict<SPAN class="punctuation token">)</SPAN> token <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>urllib<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>tokenurl <SPAN class="operator token">+</SPAN> <SPAN class="string token">"?f=json"</SPAN><SPAN class="punctuation token">,</SPAN> query_string<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="string token">"token"</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> token<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>token<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'error'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> sys<SPAN class="punctuation token">.</SPAN>exit<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># build query dictionary</SPAN> query_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">"where"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"SurveyDate IS NOT NULL"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"outFields"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"OBJECTID, Business, SurveyDate"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"orderByFields"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"SurveyDate ASC"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"returnGeometry"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"resultRecordCount"</SPAN> <SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"json"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> token<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># to select all fields use: "outFields" : "*",</SPAN> <SPAN class="comment token"># to select individual fields use comma delimited list: "outFields" : "OBJECTID, SurveyDate",</SPAN> <SPAN class="comment token"># a date certain: "where" : "SurveyDate >= DATE '2016-05-29 18:30:00'",</SPAN> <SPAN class="comment token"># if you do not want geometry: "returnGeometry" : "false",</SPAN> <SPAN class="comment token"># resultRecordCount only applies if supportsPagination is true </SPAN> <SPAN class="comment token"># results in json format</SPAN> jsonResponse <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>urlopen<SPAN class="punctuation token">(</SPAN>URL<SPAN class="punctuation token">,</SPAN> urllib<SPAN class="punctuation token">.</SPAN>urlencode<SPAN class="punctuation token">(</SPAN>query_dict<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> features <SPAN class="operator token">=</SPAN> json<SPAN class="punctuation token">.</SPAN>loads<SPAN class="punctuation token">(</SPAN>jsonResponse<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> object_pairs_hook<SPAN class="operator token">=</SPAN>collections<SPAN class="punctuation token">.</SPAN>OrderedDict<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN>u<SPAN class="string token">'features'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># print json.dumps(features, indent=4, sort_keys=False) # formatted json</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'ObjectID\tBusiness\tSurvey Date\tX-lon\tY-lat'</SPAN> <SPAN class="keyword token">for</SPAN> feature <SPAN class="keyword token">in</SPAN> features<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># AGO uses GMT/UTC, you may wish to convert to local time</SPAN> surveyTime <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'%c'</SPAN><SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">.</SPAN>localtime<SPAN class="punctuation token">(</SPAN>feature<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'attributes'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SurveyDate'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">1000</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> str<SPAN class="punctuation token">(</SPAN>feature<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'attributes'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'OBJECTID'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\t'</SPAN> <SPAN class="operator token">+</SPAN> feature<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'attributes'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Business'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> \ <SPAN class="string token">'\t'</SPAN> <SPAN class="operator token">+</SPAN> surveyTime<SPAN class="punctuation token">,</SPAN> <SPAN class="comment token"># if you want geometry; AGO returns web mercator, reproject if necessary</SPAN> <SPAN class="comment token"># print "x: " + str(feature['geometry']['x'])+ "\ty: " + str(feature['geometry']['y'])</SPAN> <SPAN class="comment token"># WGS 1984 : (4326) Lat/Lon </SPAN> <SPAN class="comment token"># WGS 1984 Web Mercator (auxiliary sphere) : (102100) or (3857) </SPAN> ptGeometry <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>PointGeometry<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>feature<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'geometry'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'x'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>feature<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'geometry'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'y'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SpatialReference<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3857</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>projectAs<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>SpatialReference<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4326</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"\t"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>ptGeometry<SPAN class="punctuation token">.</SPAN>firstPoint<SPAN class="punctuation token">.</SPAN>X<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN><SPAN class="string token">"\t"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>ptGeometry<SPAN class="punctuation token">.</SPAN>firstPoint<SPAN class="punctuation token">.</SPAN>Y<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"\nCompleted"</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.