Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- drops
  2. DROP TABLE customers;
  3. DROP TABLE products;
  4. DROP TABLE orders;
  5.  
  6. -----------------------------
  7. -- create functions
  8.  
  9. CREATE OR REPLACE FUNCTION get_age_group(birth_date DATE)
  10. RETURN VARCHAR2 IS
  11.     age NUMBER;
  12. BEGIN
  13.     age := EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM birth_date);
  14.     IF age <= 30 THEN
  15.         RETURN 'under 30';
  16.     ELSIF age <= 40 THEN
  17.         RETURN '30-40';
  18.     ELSIF age <= 50 THEN
  19.         RETURN '40-50';
  20.     ELSIF age <= 60 THEN
  21.         RETURN '50-60';
  22.     ELSIF age <= 70 THEN
  23.         RETURN '60-70';
  24.     ELSE
  25.         RETURN 'above 70';      
  26.     END IF;    
  27. END;
  28. /
  29.  
  30. CREATE OR REPLACE FUNCTION get_income_level(init_income_level VARCHAR2)
  31. RETURN VARCHAR2 IS
  32.     income_str VARCHAR2(50);
  33.     income NUMBER;
  34. BEGIN
  35.  
  36.     income_str := REGEXP_REPLACE(init_income_level, '[A-Z]: ', '');
  37.     income_str := REGEXP_REPLACE(income_str, '([0-9]+,[0-9]+ - |[A-z]+ | [A-z]+)', '');
  38.     income_str := REGEXP_REPLACE(income_str, ',', '');
  39.     income := TO_NUMBER(income_str);
  40.    
  41.     IF income < 110000 THEN
  42.         RETURN 'low';
  43.     ELSIF income < 190000 THEN
  44.         RETURN 'medium';
  45.     ELSE
  46.         RETURN 'high';
  47.     END IF;  
  48.    
  49. END;
  50. /
  51.  
  52. CREATE OR REPLACE FUNCTION fix_status(marital_status VARCHAR2)
  53. RETURN VARCHAR2 IS
  54. BEGIN
  55.    
  56.     IF      marital_status = 'Married'
  57.             OR marital_status = 'Mabsent'
  58.             OR marital_status = 'married'
  59.             OR marital_status = 'Mar-AF'
  60.         THEN
  61.         RETURN 'married';
  62.     ELSIF marital_status IS NULL THEN
  63.         RETURN 'unknown';
  64.     ELSE
  65.         RETURN 'single';
  66.     END IF;    
  67.    
  68. END;
  69. /
  70.  
  71. -----------------------------
  72. -- create tables
  73.  
  74. CREATE TABLE customers AS
  75. SELECT id customer_id, gender,
  76.         get_age_group(birth_date) agegroup, fix_status(marital_status) marital_status,
  77.         get_income_level(income_level) income_level
  78. FROM xsales.customers;
  79.  
  80. CREATE TABLE products AS
  81. SELECT p.identifier product_id, p.name productname,
  82.         c.name categoryname, p.list_price
  83. FROM xsales.products p JOIN xsales.categories c ON p.subcategory_reference = c.id;
  84.  
  85. CREATE TABLE orders AS
  86. SELECT o.id order_id, oi.product_id, o.customer_id,
  87.         o.order_finished - oi.order_date days_to_process,
  88.         oi.amount price, oi.COST, o.channel
  89. FROM xsales.orders o JOIN xsales.order_items oi ON o.id = oi.order_id;
  90.  
  91. COMMIT;
  92.  
  93. -----------------------------
  94.  
  95. /*
  96.  
  97. -- these tables are just for the tuning analysis
  98.  
  99.  
  100. DROP TABLE orders_sorted;
  101. CREATE TABLE orders_sorted AS
  102. SELECT o.id order_id, oi.product_id, o.customer_id,
  103.         o.order_finished - oi.order_date days_to_process,
  104.         oi.amount price, oi.cost, o.channel
  105. FROM xsales.orders o JOIN xsales.order_items oi ON o.id = oi.order_id
  106. ORDER BY order_id;
  107.  
  108. DROP TABLE orders_uns;
  109. CREATE TABLE orders_uns AS
  110. SELECT o.id order_id, oi.product_id, o.customer_id,
  111.         o.order_finished - oi.order_date days_to_process,
  112.         oi.amount price, oi.cost, o.channel
  113. FROM xsales.orders o JOIN xsales.order_items oi ON o.id = oi.order_id;
  114.  
  115. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement