{"id":8370,"date":"2023-09-12T06:34:19","date_gmt":"2023-09-12T11:34:19","guid":{"rendered":"https:\/\/bobbeaty.com\/wp\/?p=8370"},"modified":"2023-09-13T05:48:24","modified_gmt":"2023-09-13T10:48:24","slug":"nice-postgres-feature-lateral","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/8370","title":{"rendered":"Nice Postgres Feature: <code>LATERAL<\/code>"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2008\/05\/postgresql.jpg\" alt=\"PostgreSQL.jpg\" title=\"PostgreSQL.jpg\" border=\"0\" width=\"128\" height=\"128\" style=\"float:right; margin-left:10px;\" \/><\/p>\n<p>There are many times when you would like a sub-select query to be constrained on one of the values of the main query, but when you attempt to do that you get an error message about not being able to use the variable in this context. For example, this query:<\/p>\n<pre class=\"sql\" style=\"font-family:monospace;\">  <span style=\"color: #993333; font-weight: bold;\">SELECT<\/span> c<span style=\"color: #66cc66;\">.<\/span>id<span style=\"color: #66cc66;\">,<\/span> c<span style=\"color: #66cc66;\">.<\/span>company_name<span style=\"color: #66cc66;\">,<\/span> pb<span style=\"color: #66cc66;\">.<\/span>available<span style=\"color: #66cc66;\">,<\/span> pb<span style=\"color: #66cc66;\">.<\/span>current<span style=\"color: #66cc66;\">,<\/span>\n         date_trunc<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff0000;\">'second'<\/span><span style=\"color: #66cc66;\">,<\/span> pb<span style=\"color: #66cc66;\">.<\/span>as_of<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #993333; font-weight: bold;\">AS<\/span> as_of<span style=\"color: #66cc66;\">,<\/span> pbs<span style=\"color: #66cc66;\">.*<\/span>\n    <span style=\"color: #993333; font-weight: bold;\">FROM<\/span> companies c<span style=\"color: #66cc66;\">,<\/span> plaid_tokens pt<span style=\"color: #66cc66;\">,<\/span> plaid_balances pb<span style=\"color: #66cc66;\">,<\/span>\n         <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #993333; font-weight: bold;\">SELECT<\/span> <span style=\"color: #993333; font-weight: bold;\">SUM<\/span><span style=\"color: #66cc66;\">&#40;<\/span>available<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #993333; font-weight: bold;\">AS<\/span> all_available<span style=\"color: #66cc66;\">,<\/span>\n                 <span style=\"color: #993333; font-weight: bold;\">SUM<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #993333; font-weight: bold;\">CURRENT<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #993333; font-weight: bold;\">AS<\/span> all_current\n            <span style=\"color: #993333; font-weight: bold;\">FROM<\/span> plaid_balances <span style=\"color: #993333; font-weight: bold;\">WHERE<\/span> company_id<span style=\"color: #66cc66;\">=<\/span>c<span style=\"color: #66cc66;\">.<\/span>id<span style=\"color: #66cc66;\">&#41;<\/span> pbs\n   <span style=\"color: #993333; font-weight: bold;\">WHERE<\/span> pt<span style=\"color: #66cc66;\">.<\/span>id <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#40;<\/span>c<span style=\"color: #66cc66;\">.<\/span>additional_info<span style=\"color: #66cc66;\">-&gt;&gt;<\/span><span style=\"color: #ff0000;\">'primaryPlaidAccount'<\/span><span style=\"color: #66cc66;\">&#41;<\/span>::uuid\n     <span style=\"color: #993333; font-weight: bold;\">AND<\/span> pt<span style=\"color: #66cc66;\">.<\/span>account_id <span style=\"color: #66cc66;\">=<\/span> pb<span style=\"color: #66cc66;\">.<\/span>account_id<\/pre>\n<p>where the goal is to have a sub-select gather the <tt>sum<\/tt> of the individual columns being pulled in the main query. It's a nice thing to have, but the inability to have <tt>c.id<\/tt> used in the sub-select really makes it difficult.<\/p>\n<p>Postgres has a nice feature in <tt>LATERAL<\/tt>, where is allows the sub-select to reference these fields by changing the order of evaluation of the sub-select and doesn't penalize the performance <em>too much.<\/em><\/p>\n<pre class=\"sql\" style=\"font-family:monospace;\">  <span style=\"color: #993333; font-weight: bold;\">SELECT<\/span> c<span style=\"color: #66cc66;\">.<\/span>id<span style=\"color: #66cc66;\">,<\/span> c<span style=\"color: #66cc66;\">.<\/span>company_name<span style=\"color: #66cc66;\">,<\/span> pb<span style=\"color: #66cc66;\">.<\/span>available<span style=\"color: #66cc66;\">,<\/span> pb<span style=\"color: #66cc66;\">.<\/span>current<span style=\"color: #66cc66;\">,<\/span>\n         date_trunc<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff0000;\">'second'<\/span><span style=\"color: #66cc66;\">,<\/span> pb<span style=\"color: #66cc66;\">.<\/span>as_of<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #993333; font-weight: bold;\">AS<\/span> as_of<span style=\"color: #66cc66;\">,<\/span> pbs<span style=\"color: #66cc66;\">.*<\/span>\n    <span style=\"color: #993333; font-weight: bold;\">FROM<\/span> companies c<span style=\"color: #66cc66;\">,<\/span> plaid_tokens pt<span style=\"color: #66cc66;\">,<\/span> plaid_balances pb<span style=\"color: #66cc66;\">,<\/span>\n         lateral <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #993333; font-weight: bold;\">SELECT<\/span> <span style=\"color: #993333; font-weight: bold;\">SUM<\/span><span style=\"color: #66cc66;\">&#40;<\/span>available<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #993333; font-weight: bold;\">AS<\/span> all_available<span style=\"color: #66cc66;\">,<\/span>\n                         <span style=\"color: #993333; font-weight: bold;\">SUM<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #993333; font-weight: bold;\">CURRENT<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #993333; font-weight: bold;\">AS<\/span> all_current\n                    <span style=\"color: #993333; font-weight: bold;\">FROM<\/span> plaid_balances <span style=\"color: #993333; font-weight: bold;\">WHERE<\/span> company_id<span style=\"color: #66cc66;\">=<\/span>c<span style=\"color: #66cc66;\">.<\/span>id<span style=\"color: #66cc66;\">&#41;<\/span> pbs\n   <span style=\"color: #993333; font-weight: bold;\">WHERE<\/span> pt<span style=\"color: #66cc66;\">.<\/span>id <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#40;<\/span>c<span style=\"color: #66cc66;\">.<\/span>additional_info<span style=\"color: #66cc66;\">-&gt;&gt;<\/span><span style=\"color: #ff0000;\">'primaryPlaidAccount'<\/span><span style=\"color: #66cc66;\">&#41;<\/span>::uuid\n     <span style=\"color: #993333; font-weight: bold;\">AND<\/span> pt<span style=\"color: #66cc66;\">.<\/span>account_id <span style=\"color: #66cc66;\">=<\/span> pb<span style=\"color: #66cc66;\">.<\/span>account_id<\/pre>\n<p>This is still quick, and it saves the machinations of having to calculate the sums in a temp table, or write a function to do this... it's just a nice little trick that they put in the language. Very considerate. \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many times when you would like a sub-select query to be constrained on one of the values of the main query, but when you attempt to do that you get an error message about not being able to use the variable in this context. For example, this query: SELECT c.id, c.company_name, pb.available, pb.current, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,12],"tags":[],"class_list":["post-8370","post","type-post","status-publish","format-standard","hentry","category-coding","category-javascript-coding"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/8370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=8370"}],"version-history":[{"count":1,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/8370\/revisions"}],"predecessor-version":[{"id":8371,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/8370\/revisions\/8371"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=8370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=8370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=8370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}