इम्पाला - खण्ड के साथ

यदि कोई क्वेरी बहुत जटिल है, तो हम परिभाषित कर सकते हैं aliases जटिल भागों के लिए और उन्हें क्वेरी में शामिल करते हैं with इम्पाला का खंड।

वाक्य - विन्यास

निम्नलिखित का सिंटैक्स है with इम्पाला में खंड।

with x as (select 1), y as (select 2) (select * from x union y);

उदाहरण

मान लें कि हमारे पास एक तालिका है जिसका नाम है customers डेटाबेस में my_db और इसकी सामग्री इस प्रकार है -

[quickstart.cloudera:21000] > select * from customers;
Query: select * from customers 
+----+----------+-----+-----------+--------+ 
| id | name     | age | address   | salary | 
+----+----------+-----+-----------+--------+ 
| 1  | Ramesh   | 32  | Ahmedabad | 20000  | 
| 9  | robert   | 23  | banglore  | 28000  | 
| 2  | Khilan   | 25  | Delhi     | 15000  | 
| 4  | Chaitali | 25  | Mumbai    | 35000  | 
| 7  | ram      | 25  | chennai   | 23000  | 
| 6  | Komal    | 22  | MP        | 32000  | 
| 8  | ram      | 22  | vizag     | 31000  | 
| 5  | Hardik   | 27  | Bhopal    | 40000  | 
| 3  | kaushik  | 23  | Kota      | 30000  | 
+----+----------+-----+-----------+--------+ 
Fetched 9 row(s) in 0.59s

उसी तरह, मान लीजिए कि हमारे पास एक और टेबल है, जिसका नाम है employee और इसकी सामग्री इस प्रकार है -

[quickstart.cloudera:21000] > select * from employee; 
Query: select * from employee 
+----+---------+-----+---------+--------+ 
| id | name    | age | address | salary | 
+----+---------+-----+---------+--------+ 
| 3  | mahesh  | 54  | Chennai | 55000  | 
| 2  | ramesh  | 44  | Chennai | 50000  | 
| 4  | Rupesh  | 64  | Delhi   | 60000  | 
| 1  | subhash | 34  | Delhi   | 40000  | 
+----+---------+-----+---------+--------+ 
Fetched 4 row(s) in 0.59s

निम्नलिखित का एक उदाहरण है withइम्पाला में खंड। इस उदाहरण में, हम दोनों से रिकॉर्ड प्रदर्शित कर रहे हैंemployee तथा customers जिनकी आयु 25 से अधिक है with खंड।

[quickstart.cloudera:21000] > 
   with t1 as (select * from customers where age>25), 
   t2 as (select * from employee where age>25) 
   (select * from t1 union select * from t2);

निष्पादित करने पर, उपरोक्त क्वेरी निम्न आउटपुट देती है।

Query: with t1 as (select * from customers where age>25), t2 as (select * from employee where age>25) 
   (select * from t1 union select * from t2)
+----+---------+-----+-----------+--------+ 
| id | name    | age | address   | salary | 
+----+---------+-----+-----------+--------+ 
| 3  | mahesh  | 54  | Chennai   | 55000  | 
| 1  | subhash | 34  | Delhi     | 40000  | 
| 2  | ramesh  | 44  | Chennai   | 50000  | 
| 5  | Hardik  | 27  | Bhopal    | 40000  | 
| 4  | Rupesh  | 64  | Delhi     | 60000  | 
| 1  | Ramesh  | 32  | Ahmedabad | 20000  | 
+----+---------+-----+-----------+--------+ 
Fetched 6 row(s) in 1.73s