database - Write a PL/SQL block which will populate the RESULTS table as described below: -


i need insert results table each customer id, , number of unique products purchased customer. secondly, need update recently_purchased column of customer table 'y'(yes if have purchased product in last 12 months) or 'n' (if have not purchased product in last 12 months).

listed below tables & definitions:

table: customer columns: customer_id number, customer_name varchar2(100), recently_purchased varchar2(1) -- 'y' or 'n'  table: cust_products columns: product_id number, customer_id number, date_purchased date  table: results columns: customer_id number, product_count number  table: products column: product_id number, product_name varchar2(100) 

this have come with:

declare  get_date date;  begin  insert results (customer_id, product_count) select c.customer_id, (select count(product_id)                      cust_products p                      p.customer_id = c.customer_id) cust_products c);  select  date_purchased   get_date cust_products; if get_date < sysdate - 365     update customer     set recently_purchased =  "y"; else      update customer     set recently_purchased =  "n"; end if;   end 

please verify this. not have db software test code. thanks

    declare         get_date date;         get_id number;        cursor c1                 select date_purchased, customer_id          cust_products          date_purchased < sysdate;      begin         insert results (customer_id, product_count)         select c.customer_id,(select count(distinct p.product_id)          cust_products p         p.customer_id = c.customer_id)         customer c;          open c1;         loop          fetch c1 get_date, get_id;          if get_date > sysdate-365             update customer             set recently_purchased =  'y'             customer_id = get_id;        else           update customer             set recently_purchased =  'n'             customer_id = get_id;         end if;          exit when c1%notfound;         end loop;            close c1;      end;     // 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -