Vous êtes sur la page 1sur 58

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.1 Analytical queries Ranking


SQL> create table sales_data(prod_id number(10),cust_id number(10),time_id date,channel_id char(1),p romo_id number(10),quantity number(10),amount number(10)); Table created. SQL> insert into sales_data values(11,1,'29-jan-2013','k',10,20,200); 1 row created. SQL> insert into sales_data values(12,2,'25-feb-2012','l',11,30,300); 1 row created. SQL> insert into sales_data values(13,3,'12-mar-2011','m',12,40,700); 1 row created. SQL> insert into sales_data values(14,4,'29-jan-2011','n',13,20,500); 1 row created. SQL> insert into sales_data values(15,5,'20-feb-2012','o',14,40,200); 1 row created. SQL> insert into sales_data values(16,6,'29-oct-2013','l',15,50,400); 1 row created. SQL> insert into sales_data values(17,7,'29-jan-2012','l',16,60,600); 1 row created. SQL> insert into sales_data values(18,8,'25-jan-2011','m',17,40,300); 1 row created. SQL> insert into sales_data values(19,9,'31-dec-2013','o',18,20,500); 1 row created. SQL> insert into sales_data values(20,10,'11-jan-2013','k',19,30,200); 1 row created.

CLASS:SYMCA

ROLL NO.04

SQL> select * from sales_data; PROD_ID CUST_ID TIME_ID C PROMO_ID QUANTITY ---------- ---------- --------- - ---------- ---------- ---------11 1 29-JAN-13 k 10 20 200 12 2 25-FEB-12 l 11 30 300 13 3 12-MAR-11 m 12 40 700 14 4 29-JAN-11 n 13 20 500 15 5 20-FEB-12 o 14 40 200 16 6 29-OCT-13 l 15 50 400 17 7 29-JAN-12 l 16 60 600 18 8 25-JAN-11 m 17 40 300 19 9 31-DEC-13 o 18 20 500 20 10 11-JAN-13 k 19 30 200 10 rows selected. Questions: 1.calculate sum of amount for particular year for particular channel. select to_char(time_id,'yyyy') "year" ,channel_id,to_char(sum(amount),'999,999,999,999.99')"amount" from sales_data group by to_char(time_id,'yyyy'),channel_id; Output: year C amount ---- - ------------------2011 m 1,000.00 2011 n 500.00 2012 l 900.00 2012 o 200.00 2013 k 400.00 2013 l 400.00 2013 o 500.00 7 rows selected. 2.calculate the rank of sales per year select rank() over(order by sum(amount))"default_rank",to_char(time_id,'yyyy')"year",channel_id,to_char(sum (amount),'999,999,999,999.99')"amount"from sales_data group by to_char(time_id,'yyyy'),channel_id; default_rank year C amount ------------ ---- - ------------------1 2012 o 200.00 2 2013 k 400.00 2 2013 l 400.00 AMOUNT

CLASS:SYMCA

ROLL NO.04

4 2011 n 4 2013 o 6 2012 l 7 2011 m 7 rows selected.

500.00 500.00 900.00 1,000.00

3.find out the default rank for the sum of amount partitioned on the basis of time and determine the ranks within each partition. select rank() over(partition by(to_char(time_id,'yyyy'))order by sum(amount))"default_rank",to_char(time_id,'yyyy')"year",channel_id,to_char(sum (amount),'999,999,999,999.99')"amount"from sales_data group by to_char(time_id,'yyyy'),channel_id;

default_rank year C amount ------------ ---- - ------------------1 2011 n 500.00 2 2011 m 1,000.00 1 2012 o 200.00 2 2012 l 900.00 1 2013 k 400.00 1 2013 l 400.00 3 2013 o 500.00 7 rows selected.

4.find out the dense rank for the sum of amounts that is based on time_id and channel_id.

select dense_rank() over(order by sum(amount))"default_rank",to_char(time_id,'yyyy')"year",channel_id,to_char(sum (amount),'999,999,999,999.99')"amount"from sales_data group by to_char(time_id,'yyyy'),channel_id;

default_rank year C amount ------------ ---- - ------------------1 2012 o 200.00 2 2013 k 400.00 2 2013 l 400.00 3 2011 n 500.00 3 2013 o 500.00 4 2012 l 900.00 5 2011 m 1,000.00

CLASS:SYMCA

ROLL NO.04

7 rows selected.

5.calculate the top 5 sales made in all the years.

select * from(select rank() over(order by sum(amount)desc) default_rank,to_char(time_id,'yyyy')"year",channel_id,to_char(sum(amount),'999,999 ,999,999.99')"amount" from sales_data group by to_char(time_id,'yyyy'),channel_id) where default_rank<6;

DEFAULT_RANK year C amount ------------ ---- - ------------------1 2011 m 1,000.00 2 2012 l 900.00 3 2011 n 500.00 3 2013 o 500.00 5 2013 k 400.00 5 2013 l 400.00 6 rows selected.

6.calculate cumulative distance.

select rank() over(order by sum(amount))default_rank,cume_dist()over (order by sum(amount))default_distance, to_char(time_id,'yyyy')"year",channel_id,to_char(sum(amount),'999,999,999,999.99') "amount" from sales_data group by to_char(time_id,'yyyy'),channel_id; DEFAULT_RANK DEFAULT_DISTANCE year C amount ------------ ---------------- ---- - ------------------1 .142857143 2012 o 200.00 2 .428571429 2013 k 400.00 2 .428571429 2013 l 400.00 4 .714285714 2011 n 500.00 4 .714285714 2013 o 500.00 6 .857142857 2012 l 900.00 7 1 2011 m 1,000.00 7 rows selected.

7.calculate percent rank

CLASS:SYMCA

ROLL NO.04

select rank() over(order by sum(amount))default_rank,percent_rank()over (order by sum(amount))default_percent, to_char(time_id,'yyyy')"year",channel_id,to_char(sum(amount),'999,999,999,999.99') "amount" from sales_data group by to_char(time_id,'yyyy'),channel_id; DEFAULT_RANK DEFAULT_PERCENT year C amount ------------ --------------- ---- - ------------------1 0 2012 o 200.00 2 .166666667 2013 k 400.00 2 .166666667 2013 l 400.00 4 .5 2011 n 500.00 4 .5 2013 o 500.00 6 .833333333 2012 l 900.00 7 1 2011 m 1,000.00 7 rows selected.

8. calculate N-tile

select rank() over(order by sum(amount))default_rank,Ntile(4)over (order by sum(amount))Ntile, to_char(time_id,'yyyy')"year",channel_id, to_char(sum(amount),'999,999,999,999.99')"amount" from sales_data group by to_char(time_id,'yyyy'),channel_id; DEFAULT_RANK NTILE year C amount ------------ ---------- ---- - ------------------1 1 2012 o 200.00 2 1 2013 k 400.00 2 2 2013 l 400.00 4 2 2011 n 500.00 4 3 2013 o 500.00 6 3 2012 l 900.00 7 4 2011 m 1,000.00 7 rows selected.

9.calulate using row_number

select rank() over(order by sum(amount))default_rank,row_number()over (order by sum(amount))row_number, to_char(time_id,'yyyy')"year",channel_id, to_char(sum(amount),'999,999,999,999.99')"amount" from sales_data group by to_char(time_id,'yyyy'),channel_id;

CLASS:SYMCA

ROLL NO.04

DEFAULT_RANK ROW_NUMBER year C amount ------------ ---------- ---- - ------------------1 1 2012 o 200.00 2 2 2013 k 400.00 2 3 2013 l 400.00 4 4 2011 n 500.00 4 5 2013 o 500.00 6 6 2012 l 900.00 7 7 2011 m 1,000.00 7 rows selected.

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.2 ANALYTICAL QUERIES WINDOWING FUNCTION


CREATE TABLE: create table sales_data2(prod_id number(10),cust_id number(10),time_id date,channel_id char(1),promo_id number(10),quantity number(10),amount number(10)); SQL> insert into sales_data2 values(1,1234,'1-jan-2012','k',1,10,500); 1 row created. SQL> insert into sales_data2 values(2,1234,'2-jan-2012','k',2,10,1500); 1 row created. SQL> insert into sales_data2 values(3,1234,'3-jan-2012','k',3,10,500); 1 row created. SQL> insert into sales_data2 values(4,1234,'4-jan-2012','k',4,10,700); 1 row created. SQL> insert into sales_data2 values(5,1234,'5-jan-2012','k',5,10,800); 1 row created. SQL> insert into sales_data2 values(6,1234,'6-jan-2012','k',6,10,900); 1 row created. SQL> insert into sales_data2 values(7,1234,'7-jan-2012','k',7,10,1000); 1 row created. SQL> insert into sales_data2 values(8,1234,'8-jan-2012','k',8,10,1500); 1 row created. SQL> insert into sales_data2 values(9,1234,'9-jan-2012','k',9,10,1200); 1 row created. SQL> insert into sales_data2 values(10,1234,'10-jan-2012','k',10,10,1300); 1 row created.

CLASS:SYMCA

ROLL NO.04

SQL> select * from sales_data2; PROD_ID CUST_ID TIME_ID C PROMO_ID QUANTITY ---------- ---------- --------- - ---------- ---------- ---------1 1234 01-JAN-12 k 1 10 500 2 1234 02-JAN-12 k 2 10 1500 3 1234 03-JAN-12 k 3 10 500 4 1234 04-JAN-12 k 4 10 700 5 1234 05-JAN-12 k 5 10 800 6 1234 06-JAN-12 k 6 10 900 7 1234 07-JAN-12 k 7 10 1000 8 1234 08-JAN-12 k 8 10 1500 9 1234 09-JAN-12 k 9 10 1200 10 1234 10-JAN-12 k 10 10 1300 10 rows selected. AMOUNT

a)To find out cumulative sum of amount channelwise. select time_id,amount,channel_id,sum(amount) over(partition by channel_id order by time_id rows unbounded preceding) cum_amt from sales_data2 where cust_id = 1234 and rownum<11;

TIME_ID AMOUNT C CUM_AMT --------- ---------- - ---------01-JAN-12 500 k 500 02-JAN-12 1500 k 2000 03-JAN-12 500 k 2500 04-JAN-12 700 k 3200 05-JAN-12 800 k 4000 06-JAN-12 900 k 4900 07-JAN-12 1000 k 5900 08-JAN-12 1500 k 7400 09-JAN-12 1200 k 8600 10-JAN-12 1300 k 9900 10 rows selected.

b)find out the sales of last date channel wise. select time_id,amount,channel_id,last_value(amount) over(partition by channel_id order by time_id rows between unbounded preceding and unbounded following) last_amt from sales_data2 where cust_id = 1234 and rownum<11;

CLASS:SYMCA

ROLL NO.04

TIME_ID AMOUNT C LAST_AMT --------- ---------- - ---------01-JAN-12 500 k 1300 02-JAN-12 1500 k 1300 03-JAN-12 500 k 1300 04-JAN-12 700 k 1300 05-JAN-12 800 k 1300 06-JAN-12 900 k 1300 07-JAN-12 1000 k 1300 08-JAN-12 1500 k 1300 09-JAN-12 1200 k 1300 10-JAN-12 1300 k 1300 10 rows selected.

c) moving average

select time_id,amount,channel_id,avg(amount) over(order by time_id rows unbounded preceding) last_amt from sales_data2 where cust_id = 1234 and rownum<11;

TIME_ID AMOUNT C LAST_AMT --------- ---------- - ---------01-JAN-12 500 k 500 02-JAN-12 1500 k 1000 03-JAN-12 500 k 833.333333 04-JAN-12 700 k 800 05-JAN-12 800 k 800 06-JAN-12 900 k 816.666667 07-JAN-12 1000 k 842.857143 08-JAN-12 1500 k 925 09-JAN-12 1200 k 955.555556 10-JAN-12 1300 k 990 10 rows selected.

d)calculate seven days moving average

select time_id,amount,channel_id,avg(amount) over(order by time_id range interval '7' day preceding) last_amt from sales_data2 where cust_id = 1234 and rownum<11;

CLASS:SYMCA

ROLL NO.04

TIME_ID AMOUNT C LAST_AMT --------- ---------- - ---------01-JAN-12 500 k 500 02-JAN-12 1500 k 1000 03-JAN-12 500 k 833.333333 04-JAN-12 700 k 800 05-JAN-12 800 k 800 06-JAN-12 900 k 816.666667 07-JAN-12 1000 k 842.857143 08-JAN-12 1500 k 925 09-JAN-12 1200 k 1012.5 10-JAN-12 1300 k 987.5 10 rows selected.

e)calculate the average of previous row and current row. select time_id,amount,channel_id,avg(amount) over(order by time_id range between '1'preceding and current row) calc_amt from sales_data2 where cust_id = 1234 and rownum<11;

TIME_ID AMOUNT C CALC_AMT --------- ---------- - ---------01-JAN-12 500 k 500 02-JAN-12 1500 k 1000 03-JAN-12 500 k 1000 04-JAN-12 700 k 600 05-JAN-12 800 k 750 06-JAN-12 900 k 850 07-JAN-12 1000 k 950 08-JAN-12 1500 k 1250 09-JAN-12 1200 k 1350 10-JAN-12 1300 k 1250

10

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.3 ANALYTICAL QUERIES ROLL-UP


Example 1

create table sales(time number(4),region varchar(10),dept varchar(10),profit number(4));

insert into sales values(1996,'central','pensale',10); insert into sales values(1996,'central','booksale',10); insert into sales values(1996,'east','pensale',10); insert into sales values(1996,'east','booksale',10); insert into sales values(1996,'west','pensale',10); insert into sales values(1996,'west','booksale',10); insert into sales values(1997,'central','pensale',10); insert into sales values(1997,'central','booksale',10); insert into sales values(1997,'east','pensale',10); insert into sales values(1997,'east','booksale',10); insert into sales values(1997,'west','pensale',10); insert into sales values(1997,'west','booksale',10); SQL> select * from sales; TIME REGION DEPT ---------- ---------- ---------- ---------1996 central pensale 1996 central booksale 1996 east pensale 1996 east booksale 1996 west pensale 1996 west booksale 1997 central pensale 1997 central booksale 1997 east pensale 1997 east booksale 1997 west pensale TIME REGION DEPT ---------- ---------- ---------- ---------1997 west booksale 12 rows selected. PROFIT 10 10 10 10 10 10 10 10 10 10 10 PROFIT 10

11

CLASS:SYMCA

ROLL NO.04

Q1. Find out the total profit from sales aggregating it deptwise,regionwise and then by time using roll up.

select time,region,dept,sum(profit) as profit from sales group by rollup(time,region,dept);

TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1996 east pensale 10 1996 east booksale 10 1996 east 20 1996 west pensale 10 1996 west booksale 10 1996 west 20 1996 central pensale 10 1996 central booksale 10 1996 central 20 1996 60 1997 east pensale 10 TIME REGION DEPT ---------- ---------- ---------- ---------1997 east booksale 1997 east 20 1997 west pensale 1997 west booksale 1997 west 20 1997 central pensale 1997 central booksale 1997 central 20 1997 60 120 21 rows selected. PROFIT 10 10 10 10 10

Q.2 Find out total profit from sales aggregating only across dept and region. select time,region,dept,sum(profit) as profit from sales group by time, rollup (region,dept);

TIME REGION DEPT ---------- ---------- ---------- ----------

PROFIT

12

CLASS:SYMCA

ROLL NO.04

1996 east 1996 east 1996 east 1996 west 1996 west 1996 west 1996 central 1996 central 1996 central 1996 1997 east

pensale booksale 20 pensale booksale 20 pensale booksale 20 60 pensale

10 10 10 10 10 10

10 PROFIT 10 10 10 10 10

TIME REGION DEPT ---------- ---------- ---------- ---------1997 east booksale 1997 east 20 1997 west pensale 1997 west booksale 1997 west 20 1997 central pensale 1997 central booksale 1997 central 20 1997 60 20 rows selected.

Q3.Find out the total profit from sales aggregating it deptwise,regionwise and then by time using roll up and grouping clause. select time,region,dept,sum(profit) as profit, grouping (time) as T, (region) as R, (dept) as D from sales group by rollup(time,region,dept);

TIME REGION DEPT PROFIT TR D ---------- ---------- ---------- ---------- ---------- ---------- ---------1996 east pensale 10 0 east pensale 1996 east booksale 10 0 east booksale 1996 east 20 0 east 1996 west pensale 10 0 west pensale 1996 west booksale 10 0 west booksale 1996 west 20 0 west 1996 central pensale 10 0 central pensale 1996 central booksale 10 0 central booksale 1996 central 20 0 central 1996 60 0

13

CLASS:SYMCA

ROLL NO.04

1997 east

pensale

10

0 east

pensale

TIME REGION DEPT PROFIT TR D ---------- ---------- ---------- ---------- ---------- ---------- ---------1997 east booksale 10 0 east booksale 1997 east 20 0 east 1997 west pensale 10 0 west pensale 1997 west booksale 10 0 west booksale 1997 west 20 0 west 1997 central pensale 10 0 central pensale 1997 central booksale 10 0 central booksale 1997 central 20 0 central 1997 60 0 120 1 21 rows selected.

example 2 create table Marketingsales(time number(4),region varchar(10),dept varchar(10),profit number(4)); insert into Marketingsales values(1986,'central','pensale',20); insert into Marketingsales values(1986,'central','booksale',20); insert into Marketingsales values(1986,'east','pensale',20); insert into Marketingsales values(1986,'east','booksale',20); insert into Marketingsales values(1986,'west','pensale',20); insert into Marketingsales values(1986,'west','booksale',20); insert into Marketingsales values(1987,'central','pensale',20); insert into Marketingsales values(1987,'central','booksale',20); insert into Marketingsales values(1987,'east','pensale',20); insert into Marketingsales values(1987,'east','booksale',20); insert into Marketingsales values(1987,'west','pensale',20); insert into Marketingsales values(1987,'west','booksale',20);

SQL> select * from Marketingsales; TIME REGION DEPT ---------- ---------- ---------- ---------1986 central pensale 1986 central booksale 1986 east pensale 1986 east booksale 1986 west pensale 1986 west booksale 1987 central pensale 1987 central booksale 1987 east pensale PROFIT 20 20 20 20 20 20 20 20 20

14

CLASS:SYMCA

ROLL NO.04

1987 east 1987 west

booksale pensale

20 20 PROFIT 20

TIME REGION DEPT ---------- ---------- ---------- ---------1987 west booksale 12 rows selected.

Q1. Find out the total profit from Marketingsales aggregating it deptwise,regionwise and then by time using roll up. select time,region,dept,sum(profit) as profit from Marketingsales group by rollup(time,region,dept);

TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1986 east pensale 20 1986 east booksale 20 1986 east 40 1986 west pensale 20 1986 west booksale 20 1986 west 40 1986 central pensale 20 1986 central booksale 20 1986 central 40 1986 120 1987 east pensale 20 TIME REGION DEPT ---------- ---------- ---------- ---------1987 east booksale 1987 east 40 1987 west pensale 1987 west booksale 1987 west 40 1987 central pensale 1987 central booksale 1987 central 40 1987 120 240 21 rows selected. PROFIT 20 20 20 20 20

15

CLASS:SYMCA

ROLL NO.04

Q.2 Find out total profit from Marketingsales aggregating only across dept and region. select time,region,dept,sum(profit) as profit from Marketingsales group by time, rollup (region,dept);

TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1986 east pensale 20 1986 east booksale 20 1986 east 40 1986 west pensale 20 1986 west booksale 20 1986 west 40 1986 central pensale 20 1986 central booksale 20 1986 central 40 1986 120 1987 east pensale 20 TIME REGION DEPT ---------- ---------- ---------- ---------1987 east booksale 1987 east 40 1987 west pensale 1987 west booksale 1987 west 40 1987 central pensale 1987 central booksale 1987 central 40 1987 120 20 rows selected. PROFIT 20 20 20 20 20

Q3.Find out the total profit from Marketingsales aggregating it deptwise,regionwise and then by time using roll up and grouping clause. select time,region,dept,sum(profit) as profit, grouping (time) as T, (region) as R, (dept) as D from Marketingsales group by rollup(time,region,dept);

16

CLASS:SYMCA

ROLL NO.04

TIME REGION DEPT PROFIT TR D ---------- ---------- ---------- ---------- ---------- ---------- ---------1986 east pensale 20 0 east pensale 1986 east booksale 20 0 east booksale 1986 east 40 0 east 1986 west pensale 20 0 west pensale 1986 west booksale 20 0 west booksale 1986 west 40 0 west 1986 central pensale 20 0 central pensale 1986 central booksale 20 0 central booksale 1986 central 40 0 central 1986 120 0 1987 east pensale 20 0 east pensale TIME REGION DEPT PROFIT TR D ---------- ---------- ---------- ---------- ---------- ---------- ---------1987 east booksale 20 0 east booksale 1987 east 40 0 east 1987 west pensale 20 0 west pensale 1987 west booksale 20 0 west booksale 1987 west 40 0 west 1987 central pensale 20 0 central pensale 1987 central booksale 20 0 central booksale 1987 central 40 0 central 1987 120 0 240 1 21 rows selected. Q4.Find out aggregation of multiple dimensions or multiple combinations of multiple dimensions using cube for region,dept and time. select NVL(region,0) region,dept,time,sum(profit) as profit from Marketingsales group by cube(time,region,dept);

REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------0 240 0 pensale 120 0 booksale 120 east 80 east pensale 40 east booksale 40 west 80 west pensale 40 west booksale 40 central 80 central pensale 40

17

CLASS:SYMCA

ROLL NO.04

REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------central booksale 40 0 1986 120 0 pensale 1986 60 0 booksale 1986 60 east 1986 40 east pensale 1986 20 east booksale 1986 20 west 1986 40 west pensale 1986 20 west booksale 1986 20 central 1986 40 REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------central pensale 1986 20 central booksale 1986 20 0 1987 120 0 pensale 1987 60 0 booksale 1987 60 east 1987 40 east pensale 1987 20 east booksale 1987 20 west 1987 40 west pensale 1987 20 west booksale 1987 20 REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------central 1987 40 central pensale 1987 20 central booksale 1987 20 36 rows selected.

Q5.Find out aggregation of multiple dimensions or multiple combinations of multiple dimensions using partial cube for region,dept and time. select time,region,dept,sum(profit) as profit from Marketingsales group by time, cube (region,dept); TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1986 120 1986 pensale 60 1986 booksale 60

18

CLASS:SYMCA

ROLL NO.04

1986 east 1986 east 1986 east 1986 west 1986 west 1986 west 1986 central 1986 central

40 pensale booksale 40 pensale booksale 40 pensale 20 20 20 20 20

TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1986 central booksale 20 1987 120 1987 pensale 60 1987 booksale 60 1987 east 40 1987 east pensale 20 1987 east booksale 20 1987 west 40 1987 west pensale 20 1987 west booksale 20 1987 central 40 TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1987 central pensale 20 1987 central booksale 20 24 rows selected.

19

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.4 ANALYTICAL QUERIES CUBE


Q1.Find out aggregation of multiple dimensions or multiple combinations of multiple dimensions using cube for region,dept and time. select NVL(region,0) region,dept,time,sum(profit) as profit from sales group by cube(time,region,dept); REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------0 120 0 pensale 60 0 booksale 60 east 40 east pensale 20 east booksale 20 west 40 west pensale 20 west booksale 20 central 40 central pensale 20 REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------central booksale 20 0 1996 60 0 pensale 1996 30 0 booksale 1996 30 east 1996 20 east pensale 1996 10 east booksale 1996 10 west 1996 20 west pensale 1996 10 west booksale 1996 10 central 1996 20 REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------central pensale 1996 10 central booksale 1996 10 0 1997 60 0 pensale 1997 30 0 booksale 1997 30 east 1997 20 east pensale 1997 10 east booksale 1997 10 west 1997 20 west pensale 1997 10 west booksale 1997 10

20

CLASS:SYMCA

ROLL NO.04

REGION DEPT TIME PROFIT ---------- ---------- ---------- ---------central 1997 20 central pensale 1997 10 central booksale 1997 10 36 rows selected. Q2.Find out aggregation of multiple dimensions or multiple combinations of multiple dimensions using partial cube for region,dept and time. select time,region,dept,sum(profit) as profit from sales group by time, cube (region,dept); TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1996 60 1996 pensale 30 1996 booksale 30 1996 east 20 1996 east pensale 10 1996 east booksale 10 1996 west 20 1996 west pensale 10 1996 west booksale 10 1996 central 20 1996 central pensale 10 TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1996 central booksale 10 1997 60 1997 pensale 30 1997 booksale 30 1997 east 20 1997 east pensale 10 1997 east booksale 10 1997 west 20 1997 west pensale 10 1997 west booksale 10 1997 central 20 TIME REGION DEPT PROFIT ---------- ---------- ---------- ---------1997 central pensale 10 1997 central booksale 10 24 rows selected.

21

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.5 ABSTRACT DATA TYPES


Example 1: SQL> create type addrs_ty1 as object (street varchar2(50),state varchar2(10),zip number(10)); 2/ Type created. SQL> create type person_ty1 as object (name varchar2(25),address addrs_ty); 2/ Type created.

Table creation: SQL> create table custo(cust_id varchar2(25),persondetail person_ty); Table created. inserting values into the table SQL> insert into custo values ('1',person_ty1('kaustubh',addrs_ty('a1','wadala',410206))); 1 row created. SQL> insert into custo values ('2',person_ty1('harshad',addrs_ty('a2','nerul',410207))); 1 row created. SQL> insert into custo values ('3',person_ty1('karan',addrs_ty('a3','gujrat',410208))); 1 row created. SQL> insert into custo values ('4',person_ty1('dinesh',addrs_ty('a4','thane',410209))); 1 row created. SQL> insert into custo values ('5',person_ty1('diksha',addrs_ty('a5','nerul',410201))); 1 row created. SQL> insert into custo values ('6',person_ty1('nilam',addrs_ty('a6','byculla',410202))); 1 row created.

22

CLASS:SYMCA

ROLL NO.04

Queries SQL> select * from custo; CUST_ID ------------------------PERSONDETAIL(NAME, ADDRESS(STREET, STATE, ZIP)) -------------------------------------------------------------------------------1 PERSON_TY1('kaustubh', ADDRS_TY('a1', 'wadala', 410206)) 2 PERSON_TY1('harshad', ADDRS_TY('a2', 'nerul', 410207)) 3 PERSON_TY1('karan', ADDRS_TY('a3', 'gujrat', 410208))

CUST_ID ------------------------PERSONDETAIL(NAME, ADDRESS(STREET, STATE, ZIP)) -------------------------------------------------------------------------------4 PERSON_TY1('dinesh', ADDRS_TY('a4', 'thane', 410209)) 5 PERSON_TY1('diksha', ADDRS_TY('a5', 'nerul', 410201)) 6 PERSON_TY1('nilam', ADDRS_TY('a6', 'byculla', 410202))

6 rows selected.

main query:

SQL> select c.persondetail.address.state as state,count(*) from custo c group by(c.persondetail.addr ess.state); STATE COUNT(*) ---------- ---------byculla 1 gujrat 1 nerul 2 thane 1 wadala 1

23

CLASS:SYMCA

ROLL NO.04

Example 2: SQL> create type college as object (collegeName varchar2(50),courses varchar2(10),fee number(10)); 2 / Type created. SQL> create type stud as object (sname varchar2(25),scollege college); 2 / Type created. SQL> create table student(stud_id varchar2(25),studentdetail stud); Table created. SQL> insert into student values ('1',stud('kaustubh',college('sterling','c++',4000))); 1 row created. SQL> insert into student values ('2',stud('harshad',college('bharati','java',4000))); 1 row created. SQL> insert into student values ('3',stud('sameer',college('ymt','c',4000))); 1 row created. SQL> insert into student values ('4',stud('gaurav',college('hiray','vb',4000))); 1 row created. SQL> insert into student values ('5',stud('diksha',college('somaiya','html',4000))); 1 row created. SQL> insert into student values ('6',stud('nilam',college('sies','wt',4000))); 1 row created.

24

CLASS:SYMCA

ROLL NO.04

QUERIES: SQL> select * from student; STUD_ID ------------------------STUDENTDETAIL(SNAME, SCOLLEGE(COLLEGENAME, COURSES, FEE)) -------------------------------------------------------------------------------1 STUD('kaustubh', COLLEGE('sterling', 'c++', 4000)) 2 STUD('harshad', COLLEGE('bharati', 'java', 4000)) 3 STUD('sameer', COLLEGE('ymt', 'c', 4000))

STUD_ID ------------------------STUDENTDETAIL(SNAME, SCOLLEGE(COLLEGENAME, COURSES, FEE)) -------------------------------------------------------------------------------4 STUD('gaurav', COLLEGE('hiray', 'vb', 4000)) 5 STUD('diksha', COLLEGE('somaiya', 'html', 4000)) 6 STUD('nilam', COLLEGE('sies', 'wt', 4000))

6 rows selected. main query: SQL> select s.studentdetail.scollege.collegename as college,count(*) from student s group by(s.stude ntdetail.scollege.collegename); COLLEGE COUNT(*) -------------------------------------------------- ---------bharati 1 hiray 1 sies 1 somaiya 1 sterling 1 ymt 1 6 rows selected.

25

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.6 VARYING ARRAY


Example 1:

SQL> create or replace type smarks as varray(5) of number(5); 2 / Type created.

SQL> create table stud_details(stud_id varchar2(10) primary key,stud_name varchar2(10),mrk smarks); Table created.

SQL> insert into stud_details values('1','kaustubh',smarks(60,55,66,70,80)); 1 row created. SQL> insert into stud_details values('2','sanket',smarks(59,55,65,70,80)); 1 row created. SQL> insert into stud_details values('3','richa',smarks(60,53,66,69,79)); 1 row created. SQL> insert into stud_details values('4','mugddha',smarks(45,45,46,55,60)); 1 row created. SQL> insert into stud_details values('5','shraddha',smarks(45,48,46,59,65)); 1 row created.

SQL> select * from stud_details; STUD_ID STUD_NAME ---------- ---------MRK -------------------------------------------------------------------------------1 kaustubh SMARKS(60, 55, 66, 70, 80) 2 sanket SMARKS(59, 55, 65, 70, 80)

26

CLASS:SYMCA

ROLL NO.04

3 richa SMARKS(60, 53, 66, 69, 79)

STUD_ID STUD_NAME ---------- ---------MRK -------------------------------------------------------------------------------4 mugddha SMARKS(45, 45, 46, 55, 60) 5 shraddha SMARKS(45, 48, 46, 59, 65)

CURSOR

SQL> declare 2 cursor stude_cursor is 3 select * from stud_details; 4 begin 5 for stud_rec in stude_cursor loop 6 dbms_output.put_line('stud_name'|| stud_rec.stud_name); 7 declare result number := 0; 8 begin 9 for i in 1..stud_rec.mrk.count 10 loop 11 result := result+stud_rec.mrk(i); 12 end loop; 13 dbms_output.put_line(result/stud_rec.mrk.count); 14 end; 15 end loop; 16 end; 17 / stud_namekaustubh 66.2 stud_namesanket 65.8 stud_namericha 65.4 stud_namemugddha 50.2 stud_nameshraddha 52.6 PL/SQL procedure successfully completed.

27

CLASS:SYMCA

ROLL NO.04

Example 2:

SQL> create or replace type score as varray(5) of number(5); 2 / Type created.

SQL> create table cric_details(cric_id varchar2(10) primary key,cric_name varchar2(10),scr score); Table created.

SQL> insert into cric_details values('1','sachin',score(99,98,94,93,91)); 1 row created. SQL> insert into cric_details values('2','saurav',score(83,55,65,70,80)); 1 row created. SQL> insert into cric_details values('3','virendra',score(60,53,66,69,79)); 1 row created. SQL> insert into cric_details values('4','dhoni',score(45,45,46,55,60)); 1 row created. SQL> insert into cric_details values('5','yuvraj',score(45,48,46,59,65)); 1 row created.

SQL> select * from cric_details; CRIC_ID CRIC_NAME ---------- ---------SCR -------------------------------------------------------------------------------1 sachin SCORE(99, 98, 94, 93, 91) 2 saurav SCORE(83, 55, 65, 70, 80)

28

CLASS:SYMCA

ROLL NO.04

3 virendra SCORE(60, 53, 66, 69, 79)

CRIC_ID CRIC_NAME ---------- ---------SCR -------------------------------------------------------------------------------4 dhoni SCORE(45, 45, 46, 55, 60) 5 yuvraj SCORE(45, 48, 46, 59, 65)

CURSOR

SQL> declare 2 cursor cric_cursor is 3 select * from cric_details; 4 begin 5 for cric_rec in cric_cursor loop 6 dbms_output.put_line('cric_name'|| cric_rec.cric_name); 7 declare result number := 0; 8 begin 9 for i in 1..cric_rec.scr.count 10 loop 11 result := result+cric_rec.scr(i); 12 end loop; 13 dbms_output.put_line(result/cric_rec.scr.count); 14 end; 15 end loop; 16 end; 17 / cric_namesachin 95 cric_namesaurav 70.6 cric_namevirendra 65.4 cric_namedhoni 50.2 cric_nameyuvraj 52.6 PL/SQL procedure successfully completed.

29

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.7 NESTED TABLE


SQL> create or replace type projectlist as table of varchar2(64); 2 / Type created. SQL> create table empl2(name varchar2(10),sal number(10),project projectlist) nested table project store as projecttab; Table created.

SQL> insert into empl2 values('saurabh',12000,projectlist('ghi','hij','ijk')); 1 row created. SQL> SQL> insert into empl2 values('richa',35000,projectlist('jkl','klm','lmn')); 1 row created. SQL> SQL> insert into empl2 values('dinesh',25000,projectlist('mno','nop','opq')); 1 row created. SQL> insert into empl2 values('kaustubh',15000,projectlist('abc','bcd','cde')); 1 row created. SQL> SQL> insert into empl2 values('sandesh',5000,projectlist('def','efg','fgh')); 1 row created. SQL> select * from empl2; NAME SAL ---------- ---------PROJECT -------------------------------------------------------------------------------saurabh 12000 PROJECTLIST('ghi', 'hij', 'ijk') richa 35000 PROJECTLIST('jkl', 'klm', 'lmn') dinesh 25000 PROJECTLIST('mno', 'nop', 'opq')

30

CLASS:SYMCA

ROLL NO.04

NAME SAL ---------- ---------PROJECT -------------------------------------------------------------------------------kaustubh 15000 PROJECTLIST('abc', 'bcd', 'cde') sandesh 5000 PROJECTLIST('def', 'efg', 'fgh')

SQL> DECLARE newproject projectlist:=projectlist('xyz','yza','cde'); 2 BEGIN 3 UPDATE empl2 set project=newproject 4 where name='kaustubh'; 5 end; 6 / PL/SQL procedure successfully completed. SQL> select * from empl2; NAME SAL ---------- ---------PROJECT -------------------------------------------------------------------------------saurabh 12000 PROJECTLIST('ghi', 'hij', 'ijk') richa 35000 PROJECTLIST('jkl', 'klm', 'lmn') dinesh 25000 PROJECTLIST('mno', 'nop', 'opq')

NAME SAL ---------- ---------PROJECT -------------------------------------------------------------------------------kaustubh 15000 PROJECTLIST('xyz', 'yza', 'cde') sandesh 5000 PROJECTLIST('def', 'efg', 'fgh')

31

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.8 OVERLOADING FUNCTIONS

SQL> create or replace type employee as object(emp_no varchar2(10),ename varchar2(20),sal number(10) ,member function getsal return number,member function getsal(ndays number) return number); 2 / Type created.

SQL> create or replace type body employee as member function getsal return number is 2 begin 3 return sal; 4 end; 5 6 7 member function getsal(ndays number) 8 return number is 9 begin 10 return sal/30*ndays; 11 end; 12 end; 13 / Type body created. SQL> create table employee1 of employee; Table created. SQL> insert into employee1 values('c1','kaustubh',15000); 1 row created. SQL> insert into employee1 values('c2','ram',25000); 1 row created. SQL> insert into employee1 values('c3','sham',35000); 1 row created. SQL> insert into employee1 values('c4','raghu',5000);

32

CLASS:SYMCA

ROLL NO.04

1 row created. SQL> insert into employee1 values('c5','kedar',15000); 1 row created.

SQL> select ename,sal,e.getsal(),e.getsal(15) from employee1 e; ENAME SAL E.GETSAL() E.GETSAL(15) -------------------- ---------- ---------- -----------kaustubh 15000 15000 7500 ram 25000 25000 12500 sham 35000 35000 17500 raghu 5000 5000 2500 kedar 15000 15000 7500

33

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.9 GRANT AND REVOKE


SQL> connect system; Enter password: ***** Connected. SQL> create table empl1(id varchar2(15),name varchar2(15),dept varchar2(15)); Table created. SQL> grant all on empl1 to kaustubh; Grant succeeded. SQL> connect kaustubh; Enter password: *** Connected. SQL> insert into system.empl1 values('e001','suresh','sales'); 1 row created. SQL> insert into system.empl1 values('e002','ramesh','marketing'); 1 row created. SQL> insert into system.empl1 values('e003','dinesh','HR'); 1 row created. SQL> insert into system.empl1 values('e004','gauresh','technology'); 1 row created. SQL> insert into system.empl1 values('e005','dharmesh','research'); 1 row created.

SQL> select * from system.empl1; ID NAME DEPT --------------- --------------- --------------e001 suresh sales e002 ramesh marketing e003 dinesh HR e004 gauresh technology e005 dharmesh research

34

CLASS:SYMCA

ROLL NO.04

SQL> connect system; Enter password: ***** Connected. SQL> create view desig as select id,name,dept from empl1; View created. SQL> grant select on desig to kaustubh; Grant succeeded. SQL> connect kaustubh; Enter password: *** Connected. SQL> select * from System.desig; ID NAME DEPT --------------- --------------- --------------e001 suresh sales e002 ramesh marketing e003 dinesh HR e004 gauresh technology e005 dharmesh research SQL> connect system; Enter password: ***** Connected. SQL> grant all on empl1 to dinesh with grant option; Grant succeeded. SQL> connect dinesh; Enter password: *** Connected.

SQL> select * from System.empl1 2 ; ID NAME DEPT --------------- --------------- --------------e001 suresh sales e002 ramesh marketing e003 dinesh HR e004 gauresh technology e005 dharmesh research

35

CLASS:SYMCA

ROLL NO.04

SQL> connect system; Enter password: ***** Connected. SQL> revoke delete on empl1 from dinesh; Revoke succeeded.

SQL> revoke all on empl1 from dinesh; Revoke succeeded.

36

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.10 PARTITIONING


CREATION: create table bookshelf( title varchar2(45) primary key not null, publisher varchar2(30), category varchar2(30), rating varchar2(2) ); insert into bookshelf values() insert into bookshelf values('around the world in 80 days','McGraw Hill','fiction','A'); insert into bookshelf values('count dracula','Thomas Cook','thriller','B'); insert into bookshelf values('count of monte cristo','Wonder','drama','C'); insert into bookshelf values('artemis fowl','piegon','fiction','B'); insert into bookshelf values('the wheel of time','random house','epic','A'); insert into bookshelf values('sherlock homes','london house','fiction','A'); insert into bookshelf values('harry potter','McGraw Hill','magic','C'); insert into bookshelf values('pyramid of fire','london house','fiction','B'); insert into bookshelf values('moby dick','Thomas Cook','thriller','C'); insert into bookshelf values('robinson crusoe','Thomas Cook','slice of life','A'); insert into bookshelf values('the breaker','random house','action','A'); insert into bookshelf values('alexander cipher','piegon','fiction','B'); create tablespace part1_ts datafile 'E:\app\Administrator\product\11.2.0\dbhome_1\oradata\newfile.dbf' size 32m autoextend on next 32m maxsize 2048m extent management local; create tablespace part2_ts datafile 'E:\app\Administrator\product\11.2.0\dbhome_1\oradata\newfile1.dbf' size 32m autoextend on next 32m maxsize 2048m extent management local;

37

CLASS:SYMCA

ROLL NO.04

QUERIES: SQL> select * from bookshelf; TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----around the world in 80 days McGraw Hill fiction A count dracula B count of monte cristo C Thomas Cook thriller

Wonder

drama

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----artemis fowl piegon fiction B the wheel of time A sherlock homes A random house epic

london house

fiction

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----harry potter McGraw Hill magic C pyramid of fire B alexander cipher B london house fiction

piegon

fiction

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN -----

38

CLASS:SYMCA

ROLL NO.04

moby dick C robinson crusoe A the breaker A

Thomas Cook

thriller

Thomas Cook

splice of life

random house

action

12 rows selected. SQL> create tablespace part1_ts datafile 'C:\oracle\oradata\sims\newfile.dbf' 2 size 32m 3 autoextend on 4 next 32m maxsize 2048m 5 extent management local; SQL> create tablespace part2_ts datafile 'C:\oracle\oradata\sims\newfile1.dbf' 2 size 32m 3 autoextend on 4 next 32m maxsize 2048m 5 extent management local;

Tablespace created. SQL> create table bookshelf_hash( 2 title varchar2(30) primary key not null, 3 publisher varchar2(30), 4 category varchar2(15), 5 rating varchar(5), 6 constraint CAT_FKK_KK 7 foreign key(title) references bookshelf(title)) 8 Partition by hash(category) 9 (Partition PART01 tablespace PART1_Ts, 10 Partition PART02 tablespace PART2_Ts); SQL> create table bookshelf_range( 2 title varchar2(30) primary key not null, 3 publisher varchar2(30), 4 category varchar2(15), 5 rating varchar(5), 6 constraint CAT_RANGE 7 foreign key(title) references bookshelf(title)) 8 Partition by range(rating) 9 (Partition PART01 values less than ('B') tablespace PART1_Ts, 10 Partition PART02 values less than (MAXValue) tablespace PART2_Ts); Table created.

39

CLASS:SYMCA

ROLL NO.04

SQL> create table bookshelf_list( 2 title varchar2(30) primary key not null, 3 publisher varchar2(30), 4 category varchar2(15), 5 rating varchar(5), 6 constraint CAT_LIST 7 foreign key(title) references bookshelf(title)) 8 Partition by list(rating) 9 (Partition PART01 values ('A','C') tablespace PART1_Ts, 10 Partition PART02 values ('B') tablespace PART2_Ts); Table created.

SQL> insert into bookshelf_list select * from bookshelf; 12 rows created. SQL> select count(*) from bookshelf_list partition(PART01); COUNT(*) ---------8 SQL> select count(*) from bookshelf_list partition(PART02); COUNT(*) ---------4 SQL> select * from bookshelf_list partition(PART01); TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----around the world in 80 days McGraw Hill fiction A count of monte cristo C the wheel of time A Wonder drama

random house

epic

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN

40

CLASS:SYMCA

ROLL NO.04

----sherlock homes A harry potter C moby dick C

london house

fiction

McGraw Hill

magic

Thomas Cook

thriller

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----robinson crusoe Thomas Cook slice of life A the breaker A random house action

8 rows selected. SQL> select * from bookshelf_list partition(PART02); TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----count dracula Thomas Cook thriller B artemis fowl B pyramid of fire B piegon fiction

london house

fiction

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----alexander cipher piegon fiction B

SQL> insert into bookshelf_hash select * from bookshelf;

41

CLASS:SYMCA

ROLL NO.04

12 rows created. SQL> select count(*) from bookshelf_hash partition(PART01); COUNT(*) ---------5 SQL> select count(*) from bookshelf_hash partition(PART02); COUNT(*) ---------7 SQL> select * from bookshelf_hash partition(PART01); TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----count of monte cristo Wonder drama C the wheel of time A harry potter C random house epic

McGraw Hill

magic

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----robinson crusoe Thomas Cook slice of life A the breaker A random house action

SQL> select * from bookshelf_hash partition(PART02); TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----around the world in 80 days McGraw Hill fiction A

42

CLASS:SYMCA

ROLL NO.04

count dracula B artemis fowl B

Thomas Cook

thriller

piegon

fiction

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----sherlock homes london house fiction A pyramid of fire B moby dick C london house fiction

Thomas Cook

thriller

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----alexander cipher piegon fiction B

7 rows selected.

SQL> insert into bookshelf_range select * from bookshelf; 12 rows created. SQL> select count(*) from bookshelf_range partition(PART01); COUNT(*) ---------5 SQL> select count(*) from bookshelf_range partition(PART02); COUNT(*) ---------7

43

CLASS:SYMCA

ROLL NO.04

SQL> select * from bookshelf_range partition(PART01); TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----around the world in 80 days McGraw Hill fiction A the wheel of time A sherlock homes A random house epic

london house

fiction

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----robinson crusoe Thomas Cook slice of life A the breaker A random house action

SQL> select * from bookshelf_range partition(PART02); TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----count dracula Thomas Cook thriller B count of monte cristo C artemis fowl B Wonder drama

piegon

fiction

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----harry potter McGraw Hill magic C

44

CLASS:SYMCA

ROLL NO.04

pyramid of fire B moby dick C

london house

fiction

Thomas Cook

thriller

TITLE PUBLISHER CATEGORY ------------------------------ ------------------------------ --------------RATIN ----alexander cipher piegon fiction B

7 rows selected.

45

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.11 BITMAP INDEX


create bitmap index for the following queries 1)find sales in given city 2)find sales in given state for a given product category 3)find sales in given state 4)find sales in given state for given product name

@C:\oracle\ora92\rdbms\admin\utlxplan.sql; @C:\oracle\ora92\sqlplus\admin\plustrce.sql; TABLE 1 create table time_dimentions( time_dim_id number(10) PRIMARY KEY, year number(10), day_of_year number(10), quarter number(10), month_name varchar2(25), days_of_week varchar2(25), DTS Date ); insert into Time_dimentions values(1,2002,9,2,'February','Tuesday',SYSDATE); insert into Time_dimentions values(2,2003,20,2,'January','Wednesday',SYSDATE); insert into Time_dimentions values(3,2005,1,2,'April','Saturday',SYSDATE); insert into Time_dimentions values(4,2004,7,2,'August','Thursday',SYSDATE); insert into Time_dimentions values(5,2007,26,2,'December','Monday',SYSDATE); SQL> select * from Time_dimentions; TIME_DIM_ID YEAR DAY_OF_YEAR QUARTER MONTH_NAME ----------- ---------- ----------- ---------- ------------------------DAYS_OF_WEEK DTS ------------------------- --------1 2002 9 2 February Tuesday 13-MAR-13 2 2003 Wednesday 3 Saturday 2005 20 2 January 13-MAR-13 1 2 April 13-MAR-13

TIME_DIM_ID YEAR DAY_OF_YEAR QUARTER MONTH_NAME ----------- ---------- ----------- ---------- -------------------------

46

CLASS:SYMCA

ROLL NO.04

DAYS_OF_WEEK DTS ------------------------- --------4 2004 7 2 August Thursday 13-MAR-13 5 Monday 2007 26 2 December 13-MAR-13

TABLE 2 create table Loc_dimentions( loc_dim_id number(10) Primary Key, coun_name varchar2(25), state_name varchar2(25), city_name vaRchar2(25), DTS Date ); insert into Loc_dimentions values(101,'UK','LONDON','PARIS',SYSDATE); insert into Loc_dimentions values(102,'USA','NEW YORK','MARYLAND',SYSDATE); insert into Loc_dimentions values(103,'INDIA','MAHARASHTRA','MUMBAI',SYSDATE); insert into Loc_dimentions values(104,'AUSTRALIA','SYDNEY','QUEENS',SYSDATE); insert into Loc_dimentions values(105,'BRAZIL','RIO','PERU',SYSDATE); SQL> select * from Loc_dimentions; LOC_DIM_ID COUN_NAME STATE_NAME ---------- ------------------------- ------------------------CITY_NAME DTS ------------------------- --------101 UK LONDON PARIS 13-MAR-13 102 USA MARYLAND 103 INDIA MUMBAI NEW YORK 13-MAR-13 MAHARASHTRA 13-MAR-13

LOC_DIM_ID COUN_NAME STATE_NAME ---------- ------------------------- ------------------------CITY_NAME DTS ------------------------- --------104 AUSTRALIA SYDNEY QUEENS 13-MAR-13

47

CLASS:SYMCA

ROLL NO.04

105 BRAZIL PERU

RIO 13-MAR-13

TABLE 3 create table Pro_dimentions( pro_dim_id number(10) primary key, pro_cat_name varchar2(25), pro_subcat_name varchar2(25), pro_name varchar2(25), pro_des varchar2(25), DTS Date);

insert into Pro_dimentions values(201,'Apparel','shirt','zodiac','XL',SYSDATE); insert into Pro_dimentions values(202,'Accessory','earings','lifestyle','jewellery',SYSDATE); insert into Pro_dimentions values(203,'Apparel','trouser','westside','XL',SYSDATE); insert into Pro_dimentions values(204,'Apparel','jeans','levis','L',SYSDATE); insert into Pro_dimentions values(205,'Accessory','hand-bag','esbeda','goldplated',SYSDATE); SQL> select * from Pro_dimentions; PRO_DIM_ID PRO_CAT_NAME PRO_SUBCAT_NAME ---------- ------------------------- ------------------------PRO_NAME PRO_DES DTS ------------------------- ------------------------- --------201 Apparel shirt zodiac XL 13-MAR-13 202 Accessory lifestyle jewellery 204 Apparel levis L earings 13-MAR-13 jeans 13-MAR-13

PRO_DIM_ID PRO_CAT_NAME PRO_SUBCAT_NAME ---------- ------------------------- ------------------------PRO_NAME PRO_DES DTS ------------------------- ------------------------- --------205 Accessory hand-bag esbeda gold-plated 13-MAR-13 203 Apparel westside trouser XL 13-MAR-13

48

CLASS:SYMCA

ROLL NO.04

TABLE 4 create table sales_facts( pro_dim_id number REFERENCES Pro_dimentions, loc_dim_id number REFERENCES Loc_dimentions, time_dim_id number REFERENCES Time_dimentions, Sales number(20), DTS Date); insert into sales_facts values(202,102,2,250,sysdate); insert into sales_facts values(201,101,1,120,sysdate); insert into sales_facts values(204,104,4,150,sysdate); insert into sales_facts values(203,103,3,470,sysdate); insert into sales_facts values(205,105,5,750,sysdate);

SQL> select * from sales_facts; PRO_DIM_ID LOC_DIM_ID TIME_DIM_ID SALES DTS ---------- ---------- ----------- ---------- --------202 102 2 250 02-APR-13 201 101 1 120 02-APR-13 204 104 4 150 02-APR-13 203 103 3 470 02-APR-13 205 105 5 750 02-APR-13

QUERIES: 1)create bitmap index BCG1 on sales_facts(l.city_name) from sales_facts s, Loc_dimentions l where s.Loc_dim_id=l.loc_dim_id; Index created. SQL> select sum(s.sales) as total_sales, 2 l.city_name from sales_facts s, 3 Loc_dimentions l where s.Loc_dim_id=l.loc_dim_id and l.city_name='PARIS' gr oup by l.city_name; TOTAL_SALES CITY_NAME ----------- ------------------------120 PARIS

2)create bitmap index BCG on sales_facts(l.city_name,p.pro_des) from sales_facts s, Loc_dimentions l, Pro_dimentions p where s.Loc_dim_id=l.loc_dim_id and s.Pro_dim_id=p.pro_dim_id; SQL> create bitmap index BCG on sales_facts(l.city_name,p.pro_des) from sales_fa

49

CLASS:SYMCA

ROLL NO.04

cts s, Loc_dimentions l, Pro_dimentions p 2 where s.Loc_dim_id=l.loc_dim_id and s.Pro_dim_id=p.pro_dim_id; Index created. SQL> select sum(s.sales) as total_sales,L.state_name,p.pro_cat_name 2 from sales_facts s, 3 Loc_dimentions l ,pro_dimentions p where s.Loc_dim_id=l.loc_dim_id and l.st ate_name='LONDON' 4 AND s.pro_dim_id=p.pro_dim_id AND p.pro_cat_name='Apparel' group by l.state _name,p.pro_cat_name; TOTAL_SALES STATE_NAME PRO_CAT_NAME ----------- ------------------------- ------------------------120 LONDON Apparel

3)create bitmap index BTI on sales_facts(l.state_name) from sales_facts s, loc_dimentions l where s.loc_dim_id= l.loc_dim_id; select sum(s.sales) as total_sales, l.state_name from sales_facts s, loc_dimentions l where s.loc_dim_id=l.loc_dim_id and l.state_name='SYDNEY' group by l.state_name;

SQL> create bitmap index BTI on sales_facts(l.state_name) from sales_facts s, lo c_dimentions l 2 where s.loc_dim_id= l.loc_dim_id; Index created. SQL> select sum(s.sales) as total_sales, l.state_name from sales_facts s, loc_di mentions l 2 where s.loc_dim_id=l.loc_dim_id and l.state_name='SYDNEY' group by l.state_ name; TOTAL_SALES STATE_NAME ----------- ------------------------150 SYDNEY

4)create bitmap index BMP on sales_facts(l.city_name, p.pro_des) from sales_facts s, loc_dimentions l, pro_dimentions p where s.loc_dim_id=l.loc_dim_id AND s.pro_dim_id=p.pro_dim_id; select sum(s.sales) as total_sales, l.city_name, p.pro_des from sales_facts s, loc_dimentions l, pro_dimentions p where s.loc_dim_id=l.loc_dim_id AND l.city_name='PARIS'

50

CLASS:SYMCA

ROLL NO.04

AND s.pro_dim_id=p.pro_dim_id AND p.pro_des='XL' group by l.city_name, p.pro_des;

5)SQL> create bitmap index BMP on sales_facts(l.city_name, p.pro_des) from 2 sales_facts s, loc_dimentions l, pro_dimentions p where s.loc_dim_id=l.loc_ dim_id 3 AND s.pro_dim_id=p.pro_dim_id; Index created. SQL> select sum(s.sales) as total_sales, l.city_name, p.pro_des from sales_facts s, 2 loc_dimentions l, pro_dimentions p where s.loc_dim_id=l.loc_dim_id AND l.ci ty_name='PARIS' 3 AND s.pro_dim_id=p.pro_dim_id AND p.pro_des='XL' group by l.city_name, p.pr o_des; TOTAL_SALES CITY_NAME PRO_DES ----------- ------------------------- ------------------------120 PARIS XL

51

CLASS:SYMCA

ROLL NO.04

PRACTICAL NO.12 DATA MINING


EXCEL TABLE:
AGE SPECT_PRECAUTION astigmatic tear product lens

young prePressbyope pressbyope young prePressbyope pressbyope young prePressbyope pressbyope young prePressbyope pressbyope pressbyope prePressbyope young young prePressbyope pressbyope young prePressbyope young pressbyope prePressbyope young prePressbyope

Hypermyopia Myopia Hypermyopia Myopia Hypermyopia Hypermyopia Hypermyopia Hypermyopia Myopia Myopia Myopia Hypermyopia Myopia Hypermyopia Myopia Hypermyopia Myopia Myopia Hypermyopia Hypermyopia Myopia Hypermyopia Myopia Myopia Hypermyopia

YES NO YES NO NO NO YES YES YES NO YES NO YES NO YES NO YES NO YES YES NO NO NO YES NO

Normal Reduced Normal Normal Normal Reduced Reduced Reduced Normal Reduced Normal Reduced Normal Reduced Normal Reduced Reduced Reduced Normal Normal Reduced Normal Normal Reduced Normal

Soft Hard None Soft Hard None Hard Soft None Soft Hard None Soft Hard None None Hard Hard Soft Soft Hard Hard None Soft Hard

Eyecontact.arff
@relation eyecontact @attribute Age{young,prepressbyope,pressbyope} @attribute Spect_precaution(Hypermyopia,Myopia} @attribute astigmatic{YES,NO} @attribute tear_product{Normal,Reduced} @attribute lens{Soft,Hard,None} @data young,Hypermyopia,YES,Normal,Soft prePressbyope,Myopia,NO,Reduced,Hard pressbyope,Hypermyopia,YES,Normal,None young,Myopia,NO,Normal,Soft prePressbyope,Hypermyopia,NO,Normal,Hard pressbyope,Hypermyopia,NO,Reduced,None young,Hypermyopia,YES,Reduced,Hard prePressbyope,Hypermyopia,YES,Reduced,Soft

52

CLASS:SYMCA
pressbyope,Myopia,YES,Normal,None young,Myopia,NO,Reduced,Soft prePressbyope,Myopia,YES,Normal,Hard pressbyope,Hypermyopia,NO,Reduced,None pressbyope,Myopia,YES,Normal,Soft prePressbyope,Hypermyopia,NO,Reduced,Hard young,Myopia,YES,Normal,None young,Hypermyopia,NO,Reduced,None prePressbyope,Myopia,YES,Reduced,Hard pressbyope,Myopia,NO,Reduced,Hard young,Hypermyopia,YES,Normal,Soft prePressbyope,Hypermyopia,YES,Normal,Soft young,Myopia,NO,Reduced,Hard pressbyope,Hypermyopia,NO,Normal,Hard prePressbyope,Myopia,NO,Normal,None young,Myopia,YES,Reduced,Soft prePressbyope,Hypermyopia,NO,Normal,Hard

ROLL NO.04

documentation 03:12:16-trees.j48 === Run information === Scheme: weka.classifiers.trees.J48 -C 0.25 -M 2 Relation: eyecontact2 Instances: 24 Attributes: 5 age spectprecaution astigmatic tear_product rec_lense Test mode: 10-fold cross-validation === Classifier model (full training set) === J48 pruned tree -----------------tear_product = normal | spectprecaution = hypermyopia: soft (7.0/2.0) | spectprecaution = myopia | | age = young: none (2.0) | | age = prepressbyobe: soft (2.0/1.0) | | age = pressbyobe: none (0.0) tear_product = reduced: hard (13.0/6.0) Number of Leaves : Size of the tree : 8 5

53

CLASS:SYMCA

ROLL NO.04

Time taken to build model: 0 seconds === Stratified cross-validation === === Summary === Correctly Classified Instances 9 37.5 % Incorrectly Classified Instances 15 62.5 % Kappa statistic 0.0625 Mean absolute error 0.439 Root mean squared error 0.5257 Relative absolute error 97.7897 % Root relative squared error 110.3748 % Total Number of Instances 24 === Detailed Accuracy By Class === TP Rate 0.375 0.625 0.125 FP Rate 0.188 0.438 0.313 Precision Recall F-Measure Class 0.5 0.375 0.429 soft 0.417 0.625 0.5 hard 0.167 0.125 0.143 none

=== Confusion Matrix === a b c <-- classified as 3 3 2 | a = soft 0 5 3 | b = hard 3 4 1 | c = none

03:12:19-trees.id3 === Run information === Scheme: weka.classifiers.trees.Id3 Relation: eyecontact2 Instances: 24 Attributes: 5 age spectprecaution astigmatic tear_product rec_lense Test mode: 10-fold cross-validation === Classifier model (full training set) === Id3

54

CLASS:SYMCA

ROLL NO.04

tear_product = normal | age = young | | spectprecaution = hypermyopia | | | astigmatic = yes: soft | | | astigmatic = no: soft | | spectprecaution = myopia: none | age = prepressbyobe | | astigmatic = yes: none | | astigmatic = no | | | spectprecaution = hypermyopia: soft | | | spectprecaution = myopia: soft | age = pressbyobe: soft tear_product = reduced | spectprecaution = hypermyopia | | age = young | | | astigmatic = yes: none | | | astigmatic = no: hard | | age = prepressbyobe: hard | | age = pressbyobe | | | astigmatic = yes: soft | | | astigmatic = no: hard | spectprecaution = myopia | | age = young | | | astigmatic = yes: hard | | | astigmatic = no: soft | | age = prepressbyobe: hard | | age = pressbyobe: hard Time taken to build model: 0 seconds === Stratified cross-validation === === Summary === Correctly Classified Instances 9 37.5 % Incorrectly Classified Instances 13 54.1667 % Kappa statistic 0.1006 Mean absolute error 0.375 Root mean squared error 0.6067 Relative absolute error 91.0522 % Root relative squared error 132.9184 % UnClassified Instances 2 8.3333 % Total Number of Instances 24 === Detailed Accuracy By Class === TP Rate FP Rate Precision Recall F-Measure Class 0.375 0.286 0.429 0.375 0.4 soft 0.333 0.188 0.4 0.333 0.364 hard 0.5 0.429 0.4 0.5 0.444 none

55

CLASS:SYMCA

ROLL NO.04

=== Confusion Matrix === a b c <-- classified as 3 2 3 | a = soft 1 2 3 | b = hard 3 1 4 | c = none 03:12:25-SimpleKmeans === Run information === Scheme: weka.clusterers.SimpleKMeans -N 2 -S 10 Relation: eyecontact2 Instances: 24 Attributes: 5 age spectprecaution astigmatic tear_product rec_lense Test mode: evaluate on training data

=== Clustering model (full training set) ===

kMeans ====== Number of iterations: 3 Within cluster sum of squared errors: 42.0 Cluster centroids: Cluster 0 Mean/Mode: pressbyobe hypermyopia yes reduced hard Std Devs: N/A N/A N/A N/A N/A Cluster 1 Mean/Mode: young myopia no normal soft Std Devs: N/A N/A N/A N/A N/A

=== Evaluation on training set ===

kMeans ====== Number of iterations: 3 Within cluster sum of squared errors: 84.0

56

CLASS:SYMCA

ROLL NO.04

Cluster centroids: Cluster 0 Mean/Mode: pressbyobe hypermyopia yes reduced hard Std Devs: N/A N/A N/A N/A N/A Cluster 1 Mean/Mode: young myopia no normal soft Std Devs: N/A N/A N/A N/A N/A Clustered Instances 0 1 15 ( 63%) 9 ( 38%)

03:12:27-Apriori === Run information === Scheme: weka.associations.Apriori -N 10 -T 0 -C 0.9 -D 0.05 -U 1.0 -M 0.1 -S 1.0 -A false -c -1 Relation: eyecontact2 Instances: 24 Attributes: 5 age spectprecaution astigmatic tear_product rec_lense === Associator model (full training set) ===

Apriori ======= Minimum support: 0.1 Minimum metric <confidence>: 0.9 Number of cycles performed: 18 Generated sets of large itemsets: Size of set of large itemsets L(1): 12 Size of set of large itemsets L(2): 54 Size of set of large itemsets L(3): 77 Size of set of large itemsets L(4): 27 Size of set of large itemsets L(5): 2

57

CLASS:SYMCA

ROLL NO.04

Best rules found: 1. age=pressbyobe astigmatic=yes 4 ==> spectprecaution=hypermyopia 4 conf:(1) 2. astigmatic=yes rec_lense=soft 4 ==> spectprecaution=hypermyopia 4 conf:(1) 3. tear_product=reduced rec_lense=none 4 ==> spectprecaution=hypermyopia 4 conf:(1) 4. age=prepressbyobe tear_product=reduced 3 ==> rec_lense=hard 3 conf:(1) 5. age=pressbyobe rec_lense=soft 3 ==> spectprecaution=hypermyopia 3 conf:(1) 6. age=pressbyobe rec_lense=soft 3 ==> astigmatic=yes 3 conf:(1) 7. age=pressbyobe astigmatic=no 3 ==> tear_product=reduced 3 conf:(1) 8. spectprecaution=hypermyopia rec_lense=hard 3 ==> tear_product=reduced 3 conf:(1) 9. astigmatic=yes rec_lense=hard 3 ==> tear_product=reduced 3 conf:(1) 10. age=pressbyobe astigmatic=yes rec_lense=soft 3 ==> spectprecaution=hypermyopia 3 conf:(1)

58

Vous aimerez peut-être aussi