<?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 Exporting Subselection based on Selection in different table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/exporting-subselection-based-on-selection-in/m-p/632714#M49293</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a Table and a Feature Class. I'm trying to create a Selection on the FC based on the Selection in the Table, and export the resulting Selected Features in the FC to a new FC, but I keep getting an invalid expression error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
#Create the Variables
BlockGroups = r"C:\ArcGIS\Business Analyst\US\Demographic Data\BA_2011.gdb\esri_bg"
BG2Branch_ODCM_MDT = r"C:\ArcGIS\PTA_Test.gdb\CBSA_12420_ProForma_ODCM_MDT"

#Make a Feature Layer from the Block Groups
arcpy.MakeFeatureLayer_management(BlockGroups, "BlockGroups_lyr")

# Create unique set of StoreIDs
StoreSet = set([r[0] for r in arcpy.da.SearchCursor(BG2Branch_ODCM_MDT, ["DID"])])

# Create a for loop to iterate through the unique set of StoreIDs
for Store in StoreSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a TableView of the Table using a Where Query to make a Table View only of the records where the 'DestID' is equal to the
&amp;nbsp;&amp;nbsp;&amp;nbsp; # current Store
&amp;nbsp;&amp;nbsp;&amp;nbsp; TableView = arcpy.MakeTableView_management(BG2Branch_ODCM_MDT, "BG2Branch_ODCM_MDT_tView", '"DestID" = ' + "'" + str(Store) + "'")
&amp;nbsp;&amp;nbsp;&amp;nbsp; TableView = TableView.getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select from the BlockGroups Feature Layer, the records which have an 'ID' that matches the values held in the 'OriginID' field
&amp;nbsp;&amp;nbsp;&amp;nbsp; # in the current selection of the 'BG2Branch_ODCM_MDT' table.
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("BlockGroups_lyr", "NEW_SELECTION", '"ID" IN (SELECT "OriginID" FROM ' + str(TableView) + ")")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Export the selected Block Groups to a new Feature Class.
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureClassToFeatureClass_conversion("BlockGroups_lyr", r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb", "TradeArea_" + str(Store))
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I keep receiving the error&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="color:#ff0000;"&gt;Runtime error&amp;nbsp; Traceback (most recent call last):&amp;nbsp;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 19, in &amp;lt;module&amp;gt;&amp;nbsp;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 20 Jun 2013 19:31:45 GMT</pubDate>
    <dc:creator>JohnDye</dc:creator>
    <dc:date>2013-06-20T19:31:45Z</dc:date>
    <item>
      <title>Exporting Subselection based on Selection in different table</title>
      <link>https://community.esri.com/t5/python-questions/exporting-subselection-based-on-selection-in/m-p/632714#M49293</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a Table and a Feature Class. I'm trying to create a Selection on the FC based on the Selection in the Table, and export the resulting Selected Features in the FC to a new FC, but I keep getting an invalid expression error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
#Create the Variables
BlockGroups = r"C:\ArcGIS\Business Analyst\US\Demographic Data\BA_2011.gdb\esri_bg"
BG2Branch_ODCM_MDT = r"C:\ArcGIS\PTA_Test.gdb\CBSA_12420_ProForma_ODCM_MDT"

#Make a Feature Layer from the Block Groups
arcpy.MakeFeatureLayer_management(BlockGroups, "BlockGroups_lyr")

# Create unique set of StoreIDs
StoreSet = set([r[0] for r in arcpy.da.SearchCursor(BG2Branch_ODCM_MDT, ["DID"])])

# Create a for loop to iterate through the unique set of StoreIDs
for Store in StoreSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a TableView of the Table using a Where Query to make a Table View only of the records where the 'DestID' is equal to the
&amp;nbsp;&amp;nbsp;&amp;nbsp; # current Store
&amp;nbsp;&amp;nbsp;&amp;nbsp; TableView = arcpy.MakeTableView_management(BG2Branch_ODCM_MDT, "BG2Branch_ODCM_MDT_tView", '"DestID" = ' + "'" + str(Store) + "'")
&amp;nbsp;&amp;nbsp;&amp;nbsp; TableView = TableView.getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select from the BlockGroups Feature Layer, the records which have an 'ID' that matches the values held in the 'OriginID' field
&amp;nbsp;&amp;nbsp;&amp;nbsp; # in the current selection of the 'BG2Branch_ODCM_MDT' table.
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("BlockGroups_lyr", "NEW_SELECTION", '"ID" IN (SELECT "OriginID" FROM ' + str(TableView) + ")")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Export the selected Block Groups to a new Feature Class.
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureClassToFeatureClass_conversion("BlockGroups_lyr", r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb", "TradeArea_" + str(Store))
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I keep receiving the error&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="color:#ff0000;"&gt;Runtime error&amp;nbsp; Traceback (most recent call last):&amp;nbsp;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 19, in &amp;lt;module&amp;gt;&amp;nbsp;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Jun 2013 19:31:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/exporting-subselection-based-on-selection-in/m-p/632714#M49293</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2013-06-20T19:31:45Z</dc:date>
    </item>
  </channel>
</rss>

