<?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 Left outer join with two key fields, MakeQueryTable ? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448029#M35138</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I???m quite new to both Python and ArcGIS and I can???t figure out how to solve the following problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have two tables&amp;nbsp; (???A??? and ???B???) which both have a first key_field (???number???) and a second key field ???subnumber???.&amp;nbsp; Table ???A??? has more records than table ???B???. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Now I have to select all those records from table ???A??? that are NOT contained in table ???B??? too.&amp;nbsp; (Later on I have to loop through those records.) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried the following statement:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeQueryTable_management(tableList, ???query1???,"USE_KEY_FIELDS", key_fields_List, ??????, whereClause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;tableList: table ???A??? and ???B???&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;query1:&amp;nbsp; new Table view&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;key_fields_List:&amp;nbsp; A.number, A.subnumber,B.number, B.subnumber&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where clause: B.number is NULL&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get the following error message: An invalid SQL statement was used. [query1]. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;As soon as I run it without the key_fields_List and a where_clause, it runs ok and the two tables get joined (somehow???).&amp;nbsp; I guess it???s my key_fields_List that???s wrong??? (I tried it just with "number, subnumer" as well, but I get the same error.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone know how I have to define the different terms of the MakeQueryTable-statement? Or is there a different way how I could solve my problem?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your help&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Vera&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 16 Sep 2011 09:25:14 GMT</pubDate>
    <dc:creator>VeraDiaz-Köhli</dc:creator>
    <dc:date>2011-09-16T09:25:14Z</dc:date>
    <item>
      <title>Left outer join with two key fields, MakeQueryTable ?</title>
      <link>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448029#M35138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I???m quite new to both Python and ArcGIS and I can???t figure out how to solve the following problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have two tables&amp;nbsp; (???A??? and ???B???) which both have a first key_field (???number???) and a second key field ???subnumber???.&amp;nbsp; Table ???A??? has more records than table ???B???. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Now I have to select all those records from table ???A??? that are NOT contained in table ???B??? too.&amp;nbsp; (Later on I have to loop through those records.) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried the following statement:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeQueryTable_management(tableList, ???query1???,"USE_KEY_FIELDS", key_fields_List, ??????, whereClause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;tableList: table ???A??? and ???B???&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;query1:&amp;nbsp; new Table view&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;key_fields_List:&amp;nbsp; A.number, A.subnumber,B.number, B.subnumber&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where clause: B.number is NULL&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get the following error message: An invalid SQL statement was used. [query1]. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;As soon as I run it without the key_fields_List and a where_clause, it runs ok and the two tables get joined (somehow???).&amp;nbsp; I guess it???s my key_fields_List that???s wrong??? (I tried it just with "number, subnumer" as well, but I get the same error.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone know how I have to define the different terms of the MakeQueryTable-statement? Or is there a different way how I could solve my problem?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your help&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Vera&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Sep 2011 09:25:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448029#M35138</guid>
      <dc:creator>VeraDiaz-Köhli</dc:creator>
      <dc:date>2011-09-16T09:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: Left outer join with two key fields, MakeQueryTable ?</title>
      <link>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448030#M35139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't know if this is just a rendering thing, but your code seems to have unicode quotes (�??�?�, a.k.a. \201C, \201D), as opposed to regular quotes (""), which may cause some issues when using arcpy. I've had some minor issues in the past using these in larger scripts and tools....&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In either case, would it be possible to see the exact code you are using? It's sometimes hard to know whether there's something hiding behind metacode.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A description of how to use arcpy.MakeQueryTable_management (though with aliases) can be found on &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006r000000"&gt;ESRI's usage page for Make Query Table&lt;/A&gt;&lt;BR /&gt;&lt;SPAN&gt;A big issue to note is that your WHERE clause is being used to filter values but also to define the JOIN parameters between your two tables.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One of the examples on ESRI's page has a WHERE clause that looks like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;whereClause = "vtest.COUNTIES.FIPS = vtest.CODEMOG.Fips and vtest.COUNTIES.STATE_NAME = 'California'"&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;that both joins on COUNTIES.FIPS = CODEMOG.Fips, but also filters based on STATE_NAME = 'California'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, unless I'm mistaken, your WHERE would need to look roughly something like this&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;whereClause = "A.number = B.number and A.subnumber = B.subnumber and B.number IS NULL"&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;which should return the rows where there is no B.number matching that particular A.number (i.e. A's value is not present in B.) However, that's assuming it acts as a Left or Full Outer Join. If not, could compare the values that are still present in A.number with your original values in A.number using a Search Cursor, or some other means.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also worth checking: &lt;/SPAN&gt;&lt;A href="http://forums.esri.com/Thread.asp?c=93&amp;amp;f=1728&amp;amp;t=172344"&gt;this older thread on the same topic.&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope this helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Marc&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Sep 2011 18:04:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448030#M35139</guid>
      <dc:creator>MarcNakleh</dc:creator>
      <dc:date>2011-09-16T18:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: Left outer join with two key fields, MakeQueryTable ?</title>
      <link>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448031#M35140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Marc&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your reply, especially for the hint with the where_clause, but it still doesn't work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I use variables for the tables and fields and what confuses me now is how to build the where clause, with quotes or without.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried all kinds of versions, but none would work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get the following error messages:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used. [QueryTable1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (MakeQueryTable).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's my code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# path to the file-geodatabase&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;pfad_gdb = "H:\\Moorinventar\\FM_OBJEKTBLAETTER\\Data\FM_OBJEKTBLAETTER.gdb\\"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# the two tables within the file-geodatabase&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;table_TEILOBJ =&amp;nbsp; pfad_gdb + "FM_TEILOBJ_REV_2007_MIT_HM"&amp;nbsp;&amp;nbsp;&amp;nbsp; # table A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;table_HM_FM =&amp;nbsp; pfad_gdb + "HM_FM_VIEW"&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # table B&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# the fields to use for the join&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Field_TeilbOj_ObjNr = table_TEILOBJ+".FM_OBJ"&amp;nbsp;&amp;nbsp; # table_A.number&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Field_TeilbOj_TObjNr = table_TEILOBJ+".FM_TOBJ" # table_A.subnumber&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Field_HMFM_ObjNr = table_HM_FM+".FM_OBJ"&amp;nbsp; # table_B.number&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Field_HMFM_TObjNr = table_HM_FM+".FM_TOBJ" # table_B.subnumber&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# key field&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;key_field= Field_TeilbOj_ObjNr&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# no field list, as all the fields are needed&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# where clause&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where_clause_ohne_HM= "\""+Field_TeilbOj_ObjNr + "\" = \""+Field_HMFM_ObjNr+"\" AND \""+Field_TeilbOj_TObjNr+"\" = \""+ Field_HMFM_TObjNr+"\" AND \""+Field_HMFM_ObjNr+"\" IS NULL"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeQueryTable_management([table_TEILOBJ,table_HM_FM], "QueryTable1", "USE_KEY_FIELDS",key_field, "",where_clause_ohne_HM)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance! &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Vera&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Sep 2011 13:22:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/left-outer-join-with-two-key-fields-makequerytable/m-p/448031#M35140</guid>
      <dc:creator>VeraDiaz-Köhli</dc:creator>
      <dc:date>2011-09-19T13:22:11Z</dc:date>
    </item>
  </channel>
</rss>

