\/blogs\/tilting\/2016\/09\/27\/performance-at-a-price-file-geodatabases-and-sql-support?sr=search&searchId=c3a45d0d-24f6-481b-bcf7-cfc7493b25fc&searchIndex=0<\/A> において、ファイルジオデータベース(FGDB)をパーソナルジオデータベース(PGDB)の代替として開発する際に Esri が行った SQL サポートのトレードオフに注目しています。この記事では、ファイルジオデータベースにおける SQL サポートについてさらに学びたい方のためにいくつかのリンクが提供されています。さまざまな情報源やリンク間で内容の重複は多いものの、重要な記述が一箇所だけに存在する場合もあり、それを知っていることはエラーのトラブルシューティングやファイルジオデータベースに保存されたデータからの不正確な結果を理解しようとする際に重要となることがあります。<\/P><\/P>ファイルジオデータベースと SQL を使用する際にユーザーが問題に陥りやすい領域の一つは EXISTS 条件または演算子です。File Geodatabase API の SQL リファレンス(FileGDB_SQL.htm)(@Esri Downloads <\/A>)や ArcGIS で使用されるクエリ式の SQL リファレンス<\/A>を見ると:<\/P>[NOT] EXISTS<\/P><\/TD>サブクエリが少なくとも一つのレコードを返す場合 TRUE を返し、それ以外の場合は FALSE を返します。例えば、この式は OBJECTID フィールドに値が50である場合 TRUE を返します:<\/P>EXISTS (SELECT * FROM parcels WHERE "OBJECTID" = 50)<\/PRE>EXISTS はファイル、パーソナル、および ArcSDE ジオデータベースのみでサポートされています。<\/TD><\/TR><\/TBODY><\/TABLE><\/BLOCKQUOTE><\/P>SQL の EXISTS がファイルジオデータベースでサポートされていることがわかっても、どのようにして問題に陥るのでしょうか?残念ながら、それは予想よりも簡単で、その答えは相関サブクエリです。<\/P><\/P>相関サブクエリは EXISTS を使う際にはかなり一般的で、実際 Microsoft の EXISTS (Transact-SQL)<\/A> ドキュメントや Oracle の EXISTS Condition <\/A> ドキュメントでは少なくとも一つのコード例で相関サブクエリを使用しています。最も単純には、相関サブクエリとは外側のクエリ内の一つ以上のテーブルに関連するサブクエリです。相関サブクエリの理論と経験則はこのブログ記事の範囲を超えますが、相関サブクエリには JOIN に似た性質、あるいは少なくとも見た目上そうした性質があることを述べておきます。<\/P><\/P>以下の例は Select Max value arcpy<\/A>から適用したもので、この GeoNet の質問が数ヶ月前に私がこの問題を調査するきっかけとなりました。まずはテキストフィールドと整数フィールドを持ち、一方のテーブルがもう一方のテーブルからレコードのサブセットを含む2つの基本的なテーブル(TableA と TableB)から始めます。<\/P><\/P><\/P>>><\/SPAN>><\/SPAN> fgdb =<\/SPAN> # ファイルジオデータベースへのパス<\/SPAN>>><\/SPAN>><\/SPAN> pgdb =<\/SPAN> # パーソナルジオデータベースへのパス<\/SPAN>>><\/SPAN>><\/SPAN> egdb =<\/SPAN> # エンタープライズジオデータベースへのパス、例では SQL Server を使用<\/SPAN>>><\/SPAN>><\/SPAN> gpkg =<\/SPAN> # GeoPackageへのパス<\/SPAN>>><\/SPAN>><\/SPAN> gdbs =<\/SPAN> (<\/SPAN>fgdb, pgdb, egdb, gpkg)<\/SPAN>>><\/SPAN>><\/SPAN> >><\/SPAN>><\/SPAN> values =<\/SPAN> (<\/SPAN>(<\/SPAN>"A"<\/ class="punctuation token">)<\/SPAN>:<\/SPAN>.<\/SPAN>.<\/SPAN>.<\/SPAN> table =<\/SPAN> arcpy.<\/SPAN>CreateTable_management(<\/SPAN>gdb, <\/SPAN>table_names[<\/SPAN>i-1<\/SPAN>]<\/SPAN>)<\/SPAN>.<\/SPAN>.<\/SPAN>.<\/SPAN> arcpy.<\/SPAN>AddField_management(<\/SPAN>table, <\/SPAN>"id"<\/SPAN>, <\/SPAN>"TEXT"<\/SPAN>)<\/SPAN>.<\/SPAN>.<\/SPAN>.<\/SPAN> arcpy.<\/SPAN>AddField_management(<\/SPAN>table, <\/SPAN>"version"<\/SPAN>, <\/SPAN>"LONG"<\/SPAN>)<\/SPAN> .<\/span>.<\/span>.<\/span> with arcpy.<\/span>da.<\/span>InsertCursor(<\/span>table, <\/span>("id"<\/span>, "version"<\/span>))<\/span> as cur:<\/span>.<\/span>.<\/span>.<\/span> for value in values[::i]: . . . . . . . . . . . . . . . . . . . . . . . . . . . cur.insertRow(value). . . . . . qry = ( style="margin: 0px 0px 0.63em; padding: 0px 1rem 0px 0.5em; width: 91px; line-height: 1.5em; letter-spacing: 0.02em; font-family: 'Avenir LT W01 65 Medium', Arial, Helvetica, sans-serif; font-size: 0.87rem; font-style: normal; font-weight: 500; float: left; white-space: nowrap; position: relative; box-sizing: border-box; -webkit-font-smoothing: antialiased;" id="toc-hId--1316845959" id="toc-hId--1316845959">注意:<\/H5>Coverages、shapefiles、およびその他の非geodatabaseファイルベースのデータソースはサブクエリをサポートしていません。バージョン管理されたArcSDEフィーチャクラスおよびテーブルで実行されるサブクエリは、デルタテーブルに格納されているフィーチャを返しません。File geodatabasesはこのセクションで説明する限定的なサブクエリのサポートを提供し、personalおよびArcSDE geodatabasesは完全なサポートを提供します。personalおよびArcSDE geodatabasesのサブクエリ機能の完全なセットについては、DBMSドキュメントを参照してください。<\/P><\/DIV><\/P><\/P><\/P><\/P>サブクエリとは、別のクエリ内にネストされたクエリのことです。述語や集約関数を適用したり、別のテーブルに格納された値とデータを比較したりするために使用できます....<\/P><\/BLOCKQUOTE><\/P>File geodatabasesでのサブクエリのサポートは以下に限定されます:<\/P>IN述語。例えば:<\/LI><\/UL>"COUNTRY_NAME" NOT IN (SELECT "COUNTRY_NAME" FROM indep_countries)<\/PRE>比較演算子を用いたスカラーサブクエリ。スカラーサブクエリは単一の値を返します。例えば:<\/LI><\/UL>"GDP2006" > (SELECT MAX("GDP2005") FROM countries)<\/PRE>File geodatabasesでは、集合関数AVG、COUNT、MIN、MAX、およびSUMはスカラーサブクエリ内でのみ使用可能です。<\/SPAN><\/P>EXISTS述語。例えば:<\/LI><\/UL>EXISTS (SELECT * FROM indep_countries WHERE "COUNTRY_NAME" = 'Mexico')<\/PRE><\/BLOCKQUOTE><\/P>ドキュメントにはサブクエリの「限定的なサポート」と記載されていますが、EXISTS述語もサポートされていると明記されています。もしサブクエリ自体が問題でないならば、おそらく相関サブクエリが問題でしょう。結局のところ、すべてのドキュメント中のEXISTS例では相関サブクエリは使われていません。残念ながら、推測するしかなく、私が見つけたfile geodatabaseドキュメントには相関サブクエリについて明示的な記述がありません。<\/P><\/P>この単純な例から、file geodatabaseは相関サブクエリを用いたEXISTSで誤った結果を返すことが明らかです。この誤った結果はfile geodatabaseにおけるSQLサポートのバグなのか制限なのか?後者だと主張しようとしても、ユーザーが誤った結果を受け取る問題が残ります。本来なら相関サブクエリが未対応の場合、ユーザーにはエラーメッセージが表示されるべきだからです。<\/P><\/BODY><\/HTML>
<\/P>
ファイルジオデータベースと SQL を使用する際にユーザーが問題に陥りやすい領域の一つは EXISTS 条件または演算子です。File Geodatabase API の SQL リファレンス(FileGDB_SQL.htm)(
[NOT] EXISTS<\/P><\/TD>サブクエリが少なくとも一つのレコードを返す場合 TRUE を返し、それ以外の場合は FALSE を返します。例えば、この式は OBJECTID フィールドに値が50である場合 TRUE を返します:<\/P>EXISTS (SELECT * FROM parcels WHERE "OBJECTID" = 50)<\/PRE>EXISTS はファイル、パーソナル、および ArcSDE ジオデータベースのみでサポートされています。<\/TD><\/TR><\/TBODY><\/TABLE><\/BLOCKQUOTE><\/P>SQL の EXISTS がファイルジオデータベースでサポートされていることがわかっても、どのようにして問題に陥るのでしょうか?残念ながら、それは予想よりも簡単で、その答えは相関サブクエリです。<\/P><\/P>相関サブクエリは EXISTS を使う際にはかなり一般的で、実際 Microsoft の
[NOT] EXISTS<\/P><\/TD>
サブクエリが少なくとも一つのレコードを返す場合 TRUE を返し、それ以外の場合は FALSE を返します。例えば、この式は OBJECTID フィールドに値が50である場合 TRUE を返します:<\/P>
EXISTS (SELECT * FROM parcels WHERE "OBJECTID" = 50)<\/PRE>EXISTS はファイル、パーソナル、および ArcSDE ジオデータベースのみでサポートされています。<\/TD><\/TR><\/TBODY><\/TABLE><\/BLOCKQUOTE><\/P>SQL の EXISTS がファイルジオデータベースでサポートされていることがわかっても、どのようにして問題に陥るのでしょうか?残念ながら、それは予想よりも簡単で、その答えは相関サブクエリです。<\/P><\/P>相関サブクエリは EXISTS を使う際にはかなり一般的で、実際 Microsoft の
以下の例は
>><\/SPAN>><\/SPAN> fgdb =<\/SPAN> # ファイルジオデータベースへのパス<\/SPAN>>><\/SPAN>><\/SPAN> pgdb =<\/SPAN> # パーソナルジオデータベースへのパス<\/SPAN>>><\/SPAN>><\/SPAN> egdb =<\/SPAN> # エンタープライズジオデータベースへのパス、例では SQL Server を使用<\/SPAN>>><\/SPAN>><\/SPAN> gpkg =<\/SPAN> # GeoPackageへのパス<\/SPAN>>><\/SPAN>><\/SPAN> gdbs =<\/SPAN> (<\/SPAN>fgdb, pgdb, egdb, gpkg)<\/SPAN>>><\/SPAN>><\/SPAN> >><\/SPAN>><\/SPAN> values =<\/SPAN> (<\/SPAN>(<\/SPAN>"A"<\/ class="punctuation token">)<\/SPAN>:<\/SPAN>.<\/SPAN>.<\/SPAN>.<\/SPAN> table =<\/SPAN> arcpy.<\/SPAN>CreateTable_management(<\/SPAN>gdb, <\/SPAN>table_names[<\/SPAN>i-1<\/SPAN>]<\/SPAN>)<\/SPAN>.<\/SPAN>.<\/SPAN>.<\/SPAN> arcpy.<\/SPAN>AddField_management(<\/SPAN>table, <\/SPAN>"id"<\/SPAN>, <\/SPAN>"TEXT"<\/SPAN>)<\/SPAN>.<\/SPAN>.<\/SPAN>.<\/SPAN> arcpy.<\/SPAN>AddField_management(<\/SPAN>table, <\/SPAN>"version"<\/SPAN>, <\/SPAN>"LONG"<\/SPAN>)<\/SPAN> .<\/span>.<\/span>.<\/span> with arcpy.<\/span>da.<\/span>InsertCursor(<\/span>table, <\/span>("id"<\/span>, "version"<\/span>))<\/span> as cur:<\/span>.<\/span>.<\/span>.<\/span> for value in values[::i]: . . . . . . . . . . . . . . . . . . . . . . . . . . . cur.insertRow(value). . . . . . qry = ( style="margin: 0px 0px 0.63em; padding: 0px 1rem 0px 0.5em; width: 91px; line-height: 1.5em; letter-spacing: 0.02em; font-family: 'Avenir LT W01 65 Medium', Arial, Helvetica, sans-serif; font-size: 0.87rem; font-style: normal; font-weight: 500; float: left; white-space: nowrap; position: relative; box-sizing: border-box; -webkit-font-smoothing: antialiased;" id="toc-hId--1316845959" id="toc-hId--1316845959">注意:<\/H5>Coverages、shapefiles、およびその他の非geodatabaseファイルベースのデータソースはサブクエリをサポートしていません。バージョン管理されたArcSDEフィーチャクラスおよびテーブルで実行されるサブクエリは、デルタテーブルに格納されているフィーチャを返しません。File geodatabasesはこのセクションで説明する限定的なサブクエリのサポートを提供し、personalおよびArcSDE geodatabasesは完全なサポートを提供します。personalおよびArcSDE geodatabasesのサブクエリ機能の完全なセットについては、DBMSドキュメントを参照してください。<\/P><\/DIV><\/P><\/P><\/P><\/P>サブクエリとは、別のクエリ内にネストされたクエリのことです。述語や集約関数を適用したり、別のテーブルに格納された値とデータを比較したりするために使用できます....<\/P><\/BLOCKQUOTE><\/P>File geodatabasesでのサブクエリのサポートは以下に限定されます:<\/P>IN述語。例えば:<\/LI><\/UL>"COUNTRY_NAME" NOT IN (SELECT "COUNTRY_NAME" FROM indep_countries)<\/PRE>比較演算子を用いたスカラーサブクエリ。スカラーサブクエリは単一の値を返します。例えば:<\/LI><\/UL>"GDP2006" > (SELECT MAX("GDP2005") FROM countries)<\/PRE>File geodatabasesでは、集合関数AVG、COUNT、MIN、MAX、およびSUMはスカラーサブクエリ内でのみ使用可能です。<\/SPAN><\/P>EXISTS述語。例えば:<\/LI><\/UL>EXISTS (SELECT * FROM indep_countries WHERE "COUNTRY_NAME" = 'Mexico')<\/PRE><\/BLOCKQUOTE><\/P>ドキュメントにはサブクエリの「限定的なサポート」と記載されていますが、EXISTS述語もサポートされていると明記されています。もしサブクエリ自体が問題でないならば、おそらく相関サブクエリが問題でしょう。結局のところ、すべてのドキュメント中のEXISTS例では相関サブクエリは使われていません。残念ながら、推測するしかなく、私が見つけたfile geodatabaseドキュメントには相関サブクエリについて明示的な記述がありません。<\/P><\/P>この単純な例から、file geodatabaseは相関サブクエリを用いたEXISTSで誤った結果を返すことが明らかです。この誤った結果はfile geodatabaseにおけるSQLサポートのバグなのか制限なのか?後者だと主張しようとしても、ユーザーが誤った結果を受け取る問題が残ります。本来なら相関サブクエリが未対応の場合、ユーザーにはエラーメッセージが表示されるべきだからです。<\/P><\/BODY><\/HTML>
File Geodatabase does not support correlated subqueries,
After publishing the blog post, I got to wondering how GeoPackages are handled. It turns out, they work just fine, i.e., one can create the tables and table view from a GeoPackage and get the correct results. I went ahead and updated the code to include using a GeoPackage.
lshipman-esristaff, thanks for the inside scoop. Do you have any idea if that recognition of response differences in geodatabases means that the documentation will be updated to be a bit more explicit, that there will be fixes (next version, patches, hotfixes) to allow SQL Correlated Subqueries to work in fGDB like they work in other data formats in the future, or is it just that this as far as it goes for the time? Thanks in advance.
I plan on updating the documentation Post 10.5. Support of SQL Correlated Subqueries is not currently planned for the fGDB, but if a persuasive business case is submitted we will consider it.
Although my blog post example involved two tables, one of our most common uses for correlated subqueries is actually self-correlated subqueries:
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> fgdb <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># path to file geodatabase</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> pgdb <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># path to personal geodatabase</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> egdb <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># path to enterprise geodatabase, SQL Server used in example</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> gpkg <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># path to GeoPackage</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> gdbs <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>fgdb<SPAN class="punctuation token">,</SPAN> pgdb<SPAN class="punctuation token">,</SPAN> egdb<SPAN class="punctuation token">,</SPAN> gpkg<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> values <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"A"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"A"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"B"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"B"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"D"</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"E"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> table_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"TableA"</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">for</SPAN> gdb <SPAN class="keyword token">in</SPAN> gdbs<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> table <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CreateTable_management<SPAN class="punctuation token">(</SPAN>gdb<SPAN class="punctuation token">,</SPAN> table_name<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"id"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TEXT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"version"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LONG"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"id"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"version"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cur<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">for</SPAN> value <SPAN class="keyword token">in</SPAN> values<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> cur<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN>value<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> qry <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"NOT EXISTS (SELECT 1"</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="string token">" FROM TableA a"</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="string token">" WHERE TableA.id = a.id"</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="string token">" AND TableA.version < a.version)"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> table_view <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeTableView_management<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"table_view"</SPAN><SPAN class="punctuation token">,</SPAN> qry<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>TableToTable_conversion<SPAN class="punctuation token">(</SPAN>table_view<SPAN class="punctuation token">,</SPAN> gdb<SPAN class="punctuation token">,</SPAN> table_name <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_max"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">print</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>table_view<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="punctuation token">(</SPAN>table_view<SPAN class="punctuation token">)</SPAN> <SPAN class="number token">8</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">5</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>
The use of a self-correlated subquery allows for selecting the maximum or minimum record for a given attribute. In the example above, a self-correlated subquery is used to select the record with the maximum value of version for each value of id.
@LanceShipman
Regarding:
"File Geodatabase does not support correlated subqueries"
Does that still apply to ArcGIS Pro 3.0.3? There seems to be a lot of stuff supported in FGDBs these days, including INNER JOIN (SQL for reporting and analysis on file geodatabases) and database views. I'd like to think correlated subqueries would be supported in this modern age.
I ask because of a related post I wrote here: File Geodatabase SQL expression to get greatest n per group
Related ideas:
BUG-000156143 - An SQL query containing the EXISTS predicate validates successfully but returns incorrect results from a file geodatabase.ENH-000156158 - Raise exception when using unsupported SQL expressions in file geodatabases.
Esri Canada Case 03264937 - Exception not raised when unsupported FGDB SQL used (correlated subquery)
10-25-2016 12:35 PMI plan on updating the documentation Post 10.5. Support of SQL Correlated Subqueries is not currently planned for the fGDB, but if a persuasive business case is submitted we will consider it.
10-25-2016 12:35 PM
I would say the "greatest 1 per group" scenario is a worthwhile business case for supporting correlated subqueries in file geodatabases. Currently, there doesn't seem to be a way to do that in FGDB SQL expressions.
It's a common requirement. For example: Selecting the most recent records based on unique values in another field
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.