{"id":4634,"date":"2013-03-13T10:25:43","date_gmt":"2013-03-13T18:25:43","guid":{"rendered":"http:\/\/seedcodenext.wordpress.com\/?p=938"},"modified":"2013-03-13T10:25:43","modified_gmt":"2013-03-13T18:25:43","slug":"sql-subqueries-in-filemaker","status":"publish","type":"post","link":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/","title":{"rendered":"SQL Subqueries in FileMaker"},"content":{"rendered":"<p>I\u2019ve had some small pre-FileMaker experience with SQL and had it in my head that subqueries work in the FROM clause. \u00a0They do in MySQL etc, so I was frustrated when I couldn\u2019t get that to work in FileMaker\u2019s SQL as it\u2019s really needed in some cases.<\/p>\n<p><a href=\"http:\/\/mightylists.blogspot.com\/2010\/07\/10-famous-submarines.html\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft\" alt=\"\" src=\"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg\" width=\"320\" height=\"158\" \/><\/a><\/p>\n<p>In a current project we have a case where there is a relationship from Contacts to Loans and we wanted to see the total Loan amount from the Contact context. \u00a0However, Contacts can be associated with a Loan using multiple roles, and we only want to have the Loan amount counted once for the Contact, otherwise the total will seem inflated. \u00a0To further complicate this, the Portal is filtered, and we want to see the filtering reflected in the total. \u00a0This, I thought was a job for SQL\u2026<\/p>\n<p><!--more--><\/p>\n<p>I first thought some kind of DISTINCT statement would allow me to just summarize the amount once per loan, but you can only apply DISTINCT to the field you want Summed. \u00a0For example,<\/p>\n<blockquote><p><code>SELECT SUM ( DISTINCT \u201cLoan Amount\u201d )<br \/>\n<\/code><\/p><\/blockquote>\n<p>But this won\u2019t work, because the Loan Amount may not be unique across Loans!<\/p>\n<p>I can do this:<\/p>\n<blockquote><p><code>SELECT DISTINCT \u201cLoanID\u201d, SUM ( \u201cLoanAmount\u201d )<br \/>\n<\/code><\/p><\/blockquote>\n<p>but that gives me the first Loan Number and Amount, not a total I can use. \u00a0If I add GROUP BY I get a little closer:<\/p>\n<blockquote><p><code>SELECT DISTINCT \u201cLoanID\u201d, SUM ( \u201cLoanAmount\u201d ) FROM Loans GROUP BY \u201cLoanID\u201d<br \/>\n<\/code><\/p><\/blockquote>\n<p>but that gives me a list like this:<\/p>\n<ul>\n<li>120225 100,000<\/li>\n<li>120226 200,000<\/li>\n<\/ul>\n<p>They\u2019re distinct amounts, but I still need to parse out the second column and sum the amounts. \u00a0I think \u201cNo problem, this is a sub query and I know how to do that.\u201d \u00a0And try to get this to work:<\/p>\n<blockquote><p><code>SELECT SUM ( \u201cAmount\u201d ) FROM ( SELECT DISTINCT \u201cLoanID\u201d As Number, \u201cLoan Amount\u201d As Amount FROM LOANS )<br \/>\n<\/code><\/p><\/blockquote>\n<p>but couldn\u2019t get that to work, and indeed it looks like that syntax is not supported in FM SQL. \u00a0I came up with a clever tail custom function to total the columns from my GROUP BY query that worked\u2026and began the subtle fear of knowing that someday that 49,999 recursion limit would be hit<\/p>\n<p>After leaving it for a bit I looked at the ODBC guide again and noted that indeed subqueries are supported, but only in the WHERE clause. \u00a0After letting that sink in I realized that I could easily get the result I wanted from a single query:<\/p>\n<blockquote><p><code>SELECT SUM ( \u201cLoanAmount\u201d ) FROM Loans WHERE \u201cLoanID\u201d IN ( SELECT DISTINCT \u201cLoanID\u201d FROM Loans )<br \/>\n<\/code><\/p><\/blockquote>\n<p>and voila! \u00a0Probably 101 for real SQL folks, but I was pleased and have found references and docs on subqueries in FMSQL scarce. \u00a0Powerful stuff and no recursion limit!<\/p>\n<div id=\"jp-post-flair\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve had some small pre-FileMaker experience with SQL and had it in my head that subqueries work in the FROM clause. \u00a0They do in MySQL etc, so I was frustrated when I couldn\u2019t get that to work in FileMaker\u2019s SQL as it\u2019s really needed in some cases. In a current project we have a case where there is a relationship from Contacts to Loans and we wanted to see the total Loan amount from the Contact context. \u00a0However, Contacts can be associated with a Loan using multiple roles, and we only want to have the Loan amount counted once for the Contact, otherwise the total will seem inflated. \u00a0To further complicate this, the Portal is filtered, and we want to see the filtering reflected in the total. \u00a0This, I thought was a job for SQL\u2026<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[18],"class_list":["post-4634","post","type-post","status-publish","format-standard","hentry","category-whats-next","tag-filemaker-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Subqueries in FileMaker - SeedCode<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Subqueries in FileMaker - SeedCode\" \/>\n<meta property=\"og:description\" content=\"I\u2019ve had some small pre-FileMaker experience with SQL and had it in my head that subqueries work in the FROM clause. \u00a0They do in MySQL etc, so I was frustrated when I couldn\u2019t get that to work in FileMaker\u2019s SQL as it\u2019s really needed in some cases. In a current project we have a case where there is a relationship from Contacts to Loans and we wanted to see the total Loan amount from the Contact context. \u00a0However, Contacts can be associated with a Loan using multiple roles, and we only want to have the Loan amount counted once for the Contact, otherwise the total will seem inflated. \u00a0To further complicate this, the Portal is filtered, and we want to see the filtering reflected in the total. \u00a0This, I thought was a job for SQL\u2026\" \/>\n<meta property=\"og:url\" content=\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\" \/>\n<meta property=\"og:site_name\" content=\"SeedCode\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/seedcoder\" \/>\n<meta property=\"article:published_time\" content=\"2013-03-13T18:25:43+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg\" \/>\n<meta name=\"author\" content=\"jasonchryoung\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dayback\" \/>\n<meta name=\"twitter:site\" content=\"@dayback\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"jasonchryoung\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\"},\"author\":{\"name\":\"jasonchryoung\",\"@id\":\"https:\/\/seedcode.com\/#\/schema\/person\/21c8e63200786c642eb068377e428ebd\"},\"headline\":\"SQL Subqueries in FileMaker\",\"datePublished\":\"2013-03-13T18:25:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\"},\"wordCount\":399,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\/\/seedcode.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg\",\"keywords\":[\"FileMaker SQL\"],\"articleSection\":[\"What's Next\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\",\"url\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\",\"name\":\"SQL Subqueries in FileMaker - SeedCode\",\"isPartOf\":{\"@id\":\"https:\/\/seedcode.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg\",\"datePublished\":\"2013-03-13T18:25:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage\",\"url\":\"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg\",\"contentUrl\":\"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/seedcode.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Subqueries in FileMaker\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/seedcode.com\/#website\",\"url\":\"https:\/\/seedcode.com\/\",\"name\":\"SeedCode\",\"description\":\"Build the Software You&#039;ve Always Wanted\",\"publisher\":{\"@id\":\"https:\/\/seedcode.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/seedcode.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/seedcode.com\/#organization\",\"name\":\"SeedCode\",\"url\":\"https:\/\/seedcode.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/seedcode.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/seedcode.com\/wp-content\/uploads\/2022\/12\/SeedCodeLogo.png\",\"contentUrl\":\"https:\/\/seedcode.com\/wp-content\/uploads\/2022\/12\/SeedCodeLogo.png\",\"width\":595,\"height\":189,\"caption\":\"SeedCode\"},\"image\":{\"@id\":\"https:\/\/seedcode.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/seedcoder\",\"https:\/\/x.com\/dayback\",\"https:\/\/www.linkedin.com\/company\/seedcode\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/seedcode.com\/#\/schema\/person\/21c8e63200786c642eb068377e428ebd\",\"name\":\"jasonchryoung\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/seedcode.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"jasonchryoung\"},\"url\":\"https:\/\/seedcode.com\/author\/jasonchryoung\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Subqueries in FileMaker - SeedCode","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/","og_locale":"en_US","og_type":"article","og_title":"SQL Subqueries in FileMaker - SeedCode","og_description":"I\u2019ve had some small pre-FileMaker experience with SQL and had it in my head that subqueries work in the FROM clause. \u00a0They do in MySQL etc, so I was frustrated when I couldn\u2019t get that to work in FileMaker\u2019s SQL as it\u2019s really needed in some cases. In a current project we have a case where there is a relationship from Contacts to Loans and we wanted to see the total Loan amount from the Contact context. \u00a0However, Contacts can be associated with a Loan using multiple roles, and we only want to have the Loan amount counted once for the Contact, otherwise the total will seem inflated. \u00a0To further complicate this, the Portal is filtered, and we want to see the filtering reflected in the total. \u00a0This, I thought was a job for SQL\u2026","og_url":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/","og_site_name":"SeedCode","article_publisher":"https:\/\/www.facebook.com\/seedcoder","article_published_time":"2013-03-13T18:25:43+00:00","og_image":[{"url":"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg","type":"","width":"","height":""}],"author":"jasonchryoung","twitter_card":"summary_large_image","twitter_creator":"@dayback","twitter_site":"@dayback","twitter_misc":{"Written by":"jasonchryoung","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#article","isPartOf":{"@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/"},"author":{"name":"jasonchryoung","@id":"https:\/\/seedcode.com\/#\/schema\/person\/21c8e63200786c642eb068377e428ebd"},"headline":"SQL Subqueries in FileMaker","datePublished":"2013-03-13T18:25:43+00:00","mainEntityOfPage":{"@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/"},"wordCount":399,"commentCount":10,"publisher":{"@id":"https:\/\/seedcode.com\/#organization"},"image":{"@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage"},"thumbnailUrl":"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg","keywords":["FileMaker SQL"],"articleSection":["What's Next"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/","url":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/","name":"SQL Subqueries in FileMaker - SeedCode","isPartOf":{"@id":"https:\/\/seedcode.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage"},"image":{"@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage"},"thumbnailUrl":"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg","datePublished":"2013-03-13T18:25:43+00:00","breadcrumb":{"@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#primaryimage","url":"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg","contentUrl":"http:\/\/205.186.131.92\/wp-content\/uploads\/2013\/03\/81168.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/seedcode.com\/sql-subqueries-in-filemaker\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/seedcode.com\/"},{"@type":"ListItem","position":2,"name":"SQL Subqueries in FileMaker"}]},{"@type":"WebSite","@id":"https:\/\/seedcode.com\/#website","url":"https:\/\/seedcode.com\/","name":"SeedCode","description":"Build the Software You&#039;ve Always Wanted","publisher":{"@id":"https:\/\/seedcode.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/seedcode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/seedcode.com\/#organization","name":"SeedCode","url":"https:\/\/seedcode.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/seedcode.com\/#\/schema\/logo\/image\/","url":"https:\/\/seedcode.com\/wp-content\/uploads\/2022\/12\/SeedCodeLogo.png","contentUrl":"https:\/\/seedcode.com\/wp-content\/uploads\/2022\/12\/SeedCodeLogo.png","width":595,"height":189,"caption":"SeedCode"},"image":{"@id":"https:\/\/seedcode.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/seedcoder","https:\/\/x.com\/dayback","https:\/\/www.linkedin.com\/company\/seedcode\/"]},{"@type":"Person","@id":"https:\/\/seedcode.com\/#\/schema\/person\/21c8e63200786c642eb068377e428ebd","name":"jasonchryoung","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/seedcode.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"jasonchryoung"},"url":"https:\/\/seedcode.com\/author\/jasonchryoung\/"}]}},"_links":{"self":[{"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/posts\/4634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/comments?post=4634"}],"version-history":[{"count":0,"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/posts\/4634\/revisions"}],"wp:attachment":[{"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/media?parent=4634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/categories?post=4634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seedcode.com\/wp-json\/wp\/v2\/tags?post=4634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}