forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com> Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
103 lines
16 KiB
HTML
103 lines
16 KiB
HTML
<a name="dli_08_0325"></a><a name="dli_08_0325"></a>
|
|
|
|
<h1 class="topictitle1">JOIN</h1>
|
|
<div id="body8662426"><div class="section" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_dli_08_0106_section79790350571"><h4 class="sectiontitle">Equi-join</h4><p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p7224133093314"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_dli_08_0218_b6799512114313">Syntax</strong></p>
|
|
<div class="codecoloring" codetype="Sql" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_dli_08_0106_screen1738816545352"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">FROM</span><span class="w"> </span><span class="n">tableExpression</span><span class="w"> </span><span class="k">INNER</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">LEFT</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">RIGHT</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">FULL</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">tableExpression</span>
|
|
<span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">value11</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value21</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="n">value12</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value22</span><span class="p">]</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p14796352125112"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b16796165217517">Precautions</strong></p>
|
|
<ul id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_ul1189585716522"><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li17895145719529">Currently, only equi-joins are supported, for example, joins that have at least one conjunctive condition with an equality predicate. Arbitrary cross or theta joins are not supported.</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li7800196145313">Tables are joined in the order in which they are specified in the FROM clause. Make sure to specify tables in an order that does not yield a cross join (Cartesian product), which are not supported and would cause a query to fail.</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li571142225316">For streaming queries the required state to compute the query result might grow infinitely depending on the type of aggregation and the number of distinct grouping keys. Provide a query configuration with valid retention interval to prevent excessive state size.</li></ul>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p16528444183512"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b6767165717511">Example</strong></p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen1640053119536">SELECT *
|
|
FROM Orders INNER JOIN Product ON Orders.productId = Product.id;
|
|
|
|
SELECT *
|
|
FROM Orders LEFT JOIN Product ON Orders.productId = Product.id;
|
|
|
|
SELECT *
|
|
FROM Orders RIGHT JOIN Product ON Orders.productId = Product.id;
|
|
|
|
SELECT *
|
|
FROM Orders FULL OUTER JOIN Product ON Orders.productId = Product.id;</pre>
|
|
<div class="section" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_dli_08_0106_section196449562372"><h4 class="sectiontitle">Time-Windowed Join</h4><p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p14259618935"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b18259171815315">Function</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p194394413548">Each piece of data in a stream is joined with data in different time zones in another stream.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p8667248153416"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b248458143415">Syntax</strong></p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen1496441356">from t1 JOIN t2 ON t1.key = t2.key AND TIMEBOUND_EXPRESSIO</pre>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p565918317369"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b135961553392">Description</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p1633504653619">TIMEBOUND_EXPRESSION can be in either of the following formats:</p>
|
|
<ul id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_ul20335124619361"><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li1033584615368">L.time between LowerBound(R.time) and UpperBound(R.time)</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li11335194610361">R.time between LowerBound(L.time) and UpperBound(L.time)</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li1335154613366">Comparison expression with the time attributes (L.time/R.time)</li></ul>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p697274665417"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b2097274665411">Precautions</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_dli_08_0106_p53854443813">A time window join requires at least one equi join predicate and a join condition that limits the time of both streams.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p92008231072">For example, use two range predicates (<, <=, >=, or >), a BETWEEN predicate, or an equal predicate that compares the same type of time attributes (such as processing time and event time) in two input tables.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p16200323575">For example, the following predicate is a valid window join condition:</p>
|
|
<ul id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_ul12001232075"><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li12003231275">ltime = rtime</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li1420072318715">ltime >= rtime AND ltime < rtime + INTERVAL '10' MINUTE</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li10200223278">ltime BETWEEN rtime - INTERVAL '10' SECOND AND rtime + INTERVAL '5' SECOND</li></ul>
|
|
</div>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p1499619341680"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b1199619341987">Example</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p11289357689">Join all orders shipped within 4 hours with their associated shipments.</p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen16156049920">SELECT *
|
|
FROM Orders o, Shipments s
|
|
WHERE o.id = s.orderId AND
|
|
o.ordertime BETWEEN s.shiptime - INTERVAL '4' HOUR AND s.shiptime;</pre>
|
|
<div class="section" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_section1465152715225"><h4 class="sectiontitle">Array Expansion</h4><p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p021613362223"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b229945410130">Precautions</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p1646683972214">This clause is used to return a new row for each element in the given array. Unnesting WITH ORDINALITY is not yet supported.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p162161352142213"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b14299155491317">Example</strong></p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen175692104234">SELECT users, tag
|
|
FROM Orders CROSS JOIN UNNEST(tags) AS t (tag);</pre>
|
|
</div>
|
|
<div class="section" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_section1068413912347"><h4 class="sectiontitle">User-Defined Table Functions</h4><p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p15875105013416"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b19625142017012">Function</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p137811339353">This clause is used to join a table with the results of a table function. ach row of the left (outer) table is joined with all rows produced by the corresponding call of the table function.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p186991310113815"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b11699171033815">Precautions</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p16378021165310">A left outer join against a lateral table requires a TRUE literal in the ON clause.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p02221153165310"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b2047419530538">Example</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p1718165413818">The row of the left (outer) table is dropped, if its table function call returns an empty result.</p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen18913162154011">SELECT users, tag
|
|
FROM Orders, LATERAL TABLE(unnest_udtf(tags)) t AS tag;</pre>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p20407073409">If a table function call returns an empty result, the corresponding outer row is preserved, and the result padded with null values.</p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen20955131734114">SELECT users, tag
|
|
FROM Orders LEFT JOIN LATERAL TABLE(unnest_udtf(tags)) t AS tag ON TRUE;</pre>
|
|
</div>
|
|
<div class="section" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_section3731195981611"><h4 class="sectiontitle">Temporal Table Function Join</h4><p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p1448416271716"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b181301113111714">Function</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p18623915121710"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b18472737141710">Precautions</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p16961133861710">Currently only inner join and left outer join with temporal tables are supported.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p7280124714175"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b147514221815">Example</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p564012318187">Assuming Rates is a temporal table function, the join can be expressed in SQL as follows:</p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen254883571810">SELECT
|
|
o_amount, r_rate
|
|
FROM
|
|
Orders,
|
|
LATERAL TABLE (Rates(o_proctime))
|
|
WHERE
|
|
r_currency = o_currency;</pre>
|
|
</div>
|
|
<div class="section" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_section740735261811"><h4 class="sectiontitle">Join Temporal Tables</h4><p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p1280151913"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b1575136172012">Function</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p192211527135610">This clause is used to join the Temporal table.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p17707930174014"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b53712360403">Syntax</strong></p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen1127710403409">SELECT column-names
|
|
FROM table1 [AS <alias1>]
|
|
[LEFT] JOIN table2 FOR SYSTEM_TIME AS OF table1.proctime [AS <alias2>]
|
|
ON table1.column-name1 = table2.key-name1</pre>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p15931106124114"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b1671451412413">Description</strong></p>
|
|
<ul id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_ul825981815425"><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li172590189421"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b15852346122717">table1.proctime</strong> indicates the processing time attribute (computed column) of <strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b9959515102917">table1</strong>.</li><li id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_li5732127135019"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b9637356172917">FOR SYSTEM_TIME AS OF table1.proctime</strong> indicates that when the records in the left table are joined with the dimension table on the right, only the snapshot data is used for matching the current processing time dimension table.</li></ul>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p4708204161916"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b1694861121911">Precautions</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p56091314197">Only inner and left joins are supported for temporal tables with processing time attributes.</p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p982917239198"><strong id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_b438417212208">Example</strong></p>
|
|
<p id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_p203698356193">LatestRates is a temporal table that is materialized with the latest rate. </p>
|
|
<pre class="screen" id="dli_08_0325__en-us_topic_0000001165951927_en-us_topic_0000001128121451_screen4850145191912">SELECT
|
|
o.amout, o.currency, r.rate, o.amount * r.rate
|
|
FROM
|
|
Orders AS o
|
|
JOIN LatestRates FOR SYSTEM_TIME AS OF o.proctime AS r
|
|
ON r.currency = o.currency;</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0321.html">Data Manipulation Language (DML)</a></div>
|
|
</div>
|
|
</div>
|
|
|