Oracle SQL and PLSQL exclusive table insertion

Source: Internet
Author: User

For an online e-commerce system, we now need to simply divide the customer into three types based on the consumption amount in the order table and insert them into three tables.

Order table order (order_id number, cust_id number, amount number); small customer table small_cust (cust_id number, tot_amt number); medium customer table med_cust (cust_id number, tot_amt number ); big account table big_cust (cust_id number, tot_amt number); if the total consumption amount is less than 10000, it is classified as a small customer; if the total consumption amount is greater than 10000 and less than 50000, it is classified as a medium customer; if the total consumption amount is greater than 50000, it is classified into major customers. To meet this requirement, if we do not know the use of insert all/FIRST, we may use a PL/SQL statement to traverse the cursor returned by the query order table, then, record the data one by one to determine the table to be inserted by the customer's total consumption. You need to write three INSERT statements separately. This can also be achieved, but INSERT is far from used. FIRST is concise and efficient. The following example is implemented using insert first. Is it clear at a glance?
 
 
  1. insert first  
  2.     when tot_amount < 10000 then  
  3.     into small_cust_test  
  4.     when tot_amount >=10000 and tot_amount <50000 then  
  5.     into med_cust_test  
  6.     else  
  7.     into big_cust_test  
  8.     select cust_id,sum(amount) as tot_amount   
  9.     from order_test  
  10.     group by cust_id;  
 
 
  1. FIRST: indicates that WHEN the first when condition is met, the next record is skipped and other WHEN conditions are no longer judged.
  2. ALL: No matter whether or not the preceding WHEN condition has been met, subsequent conditions will be judged. Multiple tables may be inserted at the same time.
  Complete sample code:
 
 
  1. SQL> create table order_test (order_id number, cust_id number, amount number); 
  2.   
  3. Table created 
  4.   
  5. SQL> create table small_cust_test (cust_id number, tot_amt number); 
  6.   
  7. Table created 
  8.   
  9. SQL> create table med_cust_test (cust_id number, tot_amt number); 
  10.   
  11. Table created 
  12.   
  13. SQL> create table big_cust_test (cust_id number, tot_amt number); 
  14.   
  15. Table created 
  16.   
  17. SQL> select * from order_test order by order_id; 
  18.   
  19.   ORDER_ID    CUST_ID     AMOUNT 
  20. ---------- ---------- ---------- 
  21.          1       1001       2060 
  22.          2       1002      20060 
  23.          3       1003      30060 
  24.          4       1004      50060 
  25.          5       1004      10060 
  26.          6       1005     100060 
  27.          7       1001       2000 
  28.          8       1001       2050 
  29.   
  30. 8 rows selected 
  31.   
  32. SQL> select cust_id, sum(amount) as tot_amt from order_test group by cust_id; 
  33.   
  34.    CUST_ID    TOT_AMT 
  35. ---------- ---------- 
  36.       1003      30060 
  37.       1001       6110 
  38.       1002      20060 
  39.       1004      60120 
  40.       1005     100060 
  41.   
  42. SQL> select * from small_cust_test; 
  43.   
  44.    CUST_ID    TOT_AMT 
  45. ---------- ---------- 
  46.   
  47. SQL> select * from med_cust_test; 
  48.   
  49.    CUST_ID    TOT_AMT 
  50. ---------- ---------- 
  51.   
  52. SQL> select * from big_cust_test; 
  53.   
  54.    CUST_ID    TOT_AMT 
  55. ---------- ---------- 
  56.   
  57. SQL> insert first 
  58.   2  when tot_amount < 10000 then 
  59.   3  into small_cust_test 
  60.   4  when tot_amount >=10000 and tot_amount <50000 then 
  61.   5  into med_cust_test 
  62.   6  else 
  63.   7  into big_cust_test 
  64.   8  select cust_id,sum(amount) as tot_amount  
  65.   9  from order_test 
  66.  10  group by cust_id; 
  67.   
  68. 5 rows inserted 
  69.   
  70. SQL> select * from small_cust_test; 
  71.   
  72.    CUST_ID    TOT_AMT 
  73. ---------- ---------- 
  74.       1001       6110 
  75.   
  76. SQL> select * from med_cust_test; 
  77.   
  78.    CUST_ID    TOT_AMT 
  79. ---------- ---------- 
  80.       1003      30060 
  81.       1002      20060 
  82.   
  83. SQL> select * from big_cust_test; 
  84.   
  85.    CUST_ID    TOT_AMT 
  86. ---------- ---------- 
  87.       1004      60120 
  88.       1005     100060 
  89.   
  90. SQL>  

This article is from "the power comes from the sincere love !" Blog, please be sure to keep this source http://stevex.blog.51cto.com/4300375/1042856

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.