forked from docs/doc-exports
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com> Co-authored-by: Yang, Tong <yangtong2@huawei.com> Co-committed-by: Yang, Tong <yangtong2@huawei.com>
76 lines
6.1 KiB
HTML
76 lines
6.1 KiB
HTML
<a name="mrs_01_0962"></a><a name="mrs_01_0962"></a>
|
|
|
|
<h1 class="topictitle1">Supporting Traditional Relational Database Syntax in Hive</h1>
|
|
<div id="body1590395283794"><div class="section" id="mrs_01_0962__sb7cb687b20704f7ba8d29f22ca173e8c"><h4 class="sectiontitle">Overview</h4><p id="mrs_01_0962__ae25dbf9b555d474bbe1f3d48361d61e6">Hive supports the following types of traditional relational database syntax:</p>
|
|
<ul id="mrs_01_0962__u42f6b64c3fa2415e8a66ffcb045e4bb0"><li id="mrs_01_0962__l6fd34038756a4e128e4deb831756dfc2">Grouping</li><li id="mrs_01_0962__laf66bfc71bdb42238eb039e1c9090ee6">EXCEPT and INTERSECT</li></ul>
|
|
</div>
|
|
<div class="section" id="mrs_01_0962__sdc0e4ea067274eccbdf64ac9dcb45fc9"><h4 class="sectiontitle">Grouping</h4><p id="mrs_01_0962__a9c2d6cff25c0446b85c5675208d1a173">Syntax description:</p>
|
|
<ul id="mrs_01_0962__uc8c4cd4a286149a1aec4db8e7c774e33"><li id="mrs_01_0962__l7007df9abc6243bda8e3a163829f99a8">Grouping takes effect only when the Group by statement contains ROLLUP or CUBE.</li><li id="mrs_01_0962__l6fc8aec385cc4080960531cc67c631e7">The result set generated by CUBE contains all the combinations of values in the selected columns.</li><li id="mrs_01_0962__l077001b2740344b68973ad7be61d5ef6">The result set generated by ROLLUP contains the combinations of a certain layer structure in the selected columns.</li><li id="mrs_01_0962__l984d6625c3bf4c34bb33750e8e0d326d">Grouping: If a row is added by using the CUBE or ROLLUP operator, the output value of the added row is 1. If the row is not added by using the CUBE or ROLLUP operator, the output value of the added row is 0.</li></ul>
|
|
<p id="mrs_01_0962__af9ca107311d4449c91c2fbf0e25f3e7c">For example, the <strong id="mrs_01_0962__a93e4767fc92d4e12af2896525044d974">table_test</strong> table exists in Hive and the table structure is as follows:</p>
|
|
<pre class="screen" id="mrs_01_0962__sadfe219cd2e44ae9b41637f7395329a9">+----------------+-------------------+--+
|
|
| table_test.id | table_test.value |
|
|
+----------------+-------------------+--+
|
|
| 1 | 10 |
|
|
| 1 | 15 |
|
|
| 2 | 20 |
|
|
| 2 | 5 |
|
|
| 2 | 13 |
|
|
+----------------+-------------------+--+</pre>
|
|
<p id="mrs_01_0962__a18d41ff7bb5a4662ac7cc506bb34a816">Run the following statement:</p>
|
|
<p id="mrs_01_0962__a565a340e3e7c4cba87d7f6aced1f4f7d"><strong id="mrs_01_0962__a2cd940ead9e74f29b52fd31c9601469d">select id,grouping(id),sum(value) from table_test group by id with rollup;</strong></p>
|
|
<p id="mrs_01_0962__ad40057bd9c9d450c905f0f2ace374c30">The result is as follows:</p>
|
|
<pre class="screen" id="mrs_01_0962__s00851b6cfd6f43979cf94c2155f8bf59">+-------+-----------------+------+--+
|
|
| id | groupingresult | sum |
|
|
+-------+-----------------+------+--+
|
|
| 1 | 0 | 25 |
|
|
| NULL | 1 | 63 |
|
|
| 2 | 0 | 38 |
|
|
+-------+-----------------+------+--+</pre>
|
|
</div>
|
|
<div class="section" id="mrs_01_0962__sec5b37310efc4d2091b2405044189282"><h4 class="sectiontitle">EXCEPT and INTERSECT</h4><p id="mrs_01_0962__a038a1c45ec714551bbbefe53f44d3113">Syntax description:</p>
|
|
<ul id="mrs_01_0962__uaab1b06dbf404e5092d6e9a66c799d3d"><li id="mrs_01_0962__l08c573fce13843cdb39fd6a40c8c4121">EXCEPT returns the difference of two result sets (that is, non-duplicated values return only one query).</li><li id="mrs_01_0962__l4dee0d95c5c549ac841feba6167c6721">INTERSECT returns the intersection of two result sets (that is, non-duplicated values return by both queries).</li></ul>
|
|
<p id="mrs_01_0962__a3ae8c66a30ac4f96ad1d6edf073d468d">For example, two tables <strong id="mrs_01_0962__aaa5c94ad242440098eb8d8e9fe5f1ce2">test_table1</strong> and <strong id="mrs_01_0962__aa66c1545474049b494e512da7a008b62">test_table2</strong> exist in Hive.</p>
|
|
<p id="mrs_01_0962__a6580ab0e37434f3d9fa126c9934f2748">The table structure of <strong id="mrs_01_0962__a7a0f39591f644a58b37fbbfb9966f3e2">test_table1</strong> is as follows:</p>
|
|
<pre class="screen" id="mrs_01_0962__s90d71c2abec9484cb10eeca21e7f6e79">+-----------------+--+
|
|
| test_table1.id |
|
|
+-----------------+--+
|
|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
+-----------------+--+</pre>
|
|
<p id="mrs_01_0962__afd33350d7c1a457692528009bce226ff">The table structure of <strong id="mrs_01_0962__abc1cbd1d58c94852bcaf92f4d657f8b8">test_table2</strong> is as follows:</p>
|
|
<pre class="screen" id="mrs_01_0962__s4a767ccb29fc41e697c99772839caa57">+-----------------+--+
|
|
| test_table2.id |
|
|
+-----------------+--+
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
+-----------------+--+</pre>
|
|
<ul id="mrs_01_0962__uc394d4e9f0f04b46af09be25a37ed4bc"><li id="mrs_01_0962__l207322bdf09e46418a0810d187cdae14">Run the following EXCEPT statement:<p id="mrs_01_0962__aae77d9b08b6f472ca350653eaf8ae14c"><a name="mrs_01_0962__l207322bdf09e46418a0810d187cdae14"></a><a name="l207322bdf09e46418a0810d187cdae14"></a><strong id="mrs_01_0962__a9e70ff9ec1a24f3ca4a624111fb7a1cd">select id from test_table1 except select id from test_table2;</strong></p>
|
|
<p id="mrs_01_0962__afcfd22cb99bc46508be4f88d6e644289">The result is as follows:</p>
|
|
<pre class="screen" id="mrs_01_0962__sa9fdd834329b48e996540219d6845067">+--------------+--+
|
|
| _alias_0.id |
|
|
+--------------+--+
|
|
| 1 |
|
|
+--------------+--+</pre>
|
|
</li><li id="mrs_01_0962__l25c1b07293c24a558f6a2ee64e3dc541">Run the following INTERSECT statement:<p id="mrs_01_0962__a8bb76dad4a65434aaa04439d660e6a2a"><a name="mrs_01_0962__l25c1b07293c24a558f6a2ee64e3dc541"></a><a name="l25c1b07293c24a558f6a2ee64e3dc541"></a><strong id="mrs_01_0962__a0657a4a567a644d78a7651afc2359e11">select id from test_table1 intersect select id from test_table2;</strong></p>
|
|
<p id="mrs_01_0962__ae96bb3f51e9f43338b3befc41297ed67">The result is as follows:</p>
|
|
<pre class="screen" id="mrs_01_0962__s5a2e70fd9e0f496bb4315eaa76177156">+--------------+--+
|
|
| _alias_0.id |
|
|
+--------------+--+
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
+--------------+--+</pre>
|
|
</li></ul>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_0581.html">Using Hive</a></div>
|
|
</div>
|
|
</div>
|
|
|