Analytical Functions Practical Page 1 of 2
Analytical Functions Practical Page 1 of 2
Analytical Functions Practical Page 1 of 2
Data model:
into
into
into
into
into
into
into
into
into
into
into
time
time
time
time
time
time
time
time
time
time
time
values(1,1,1,'MAR','Q1',2014);
values(2,2,1,'FEB','Q1',2014);
values(3,3,1,'MAR','Q1',2014);
values(4,4,1,'APR','Q2',2014);
values(5,1,1,'MAY','Q2',2014);
values(6,4,1,'NOV','Q4',2014);
values(7,1,1,'JUL','Q3',2014);
values(8,2,2,'APR','Q2',2013);
values(9,6,3,'MAY','Q2',2013);
values(10,1,2,'NOV','Q4',2013);
values(11,15,3,'JUL','Q3',2013);
INSERT
INSERT
INSERT
INSERT
INTO
INTO
INTO
INTO
ITEM
ITEM
ITEM
ITEM
VALUES('I004','SHAMPOO','HL','C','REGULAR');
VALUES('I005','BABY OIL','PnG','S','ADHOC');
VALUES('I006','PERFUME','HL','C','REGULAR');
VALUES('I007','SOAP','PnG','S','REGULAR');
into
into
into
into
into
branch
branch
branch
branch
branch
values('B001','SOUTH MUMBAI','MAIN');
values('B002','NORTH MUMBAI','NRI');
values('B003','CENTRAL MUMBAI','MAIN');
values('B004','CITY','MAIN');
values('B005','CENTRAL MUMBAI','NRI');
INTO
INTO
INTO
INTO
INTO
LOCATION
LOCATION
LOCATION
LOCATION
LOCATION
VALUES(1001,'MG ROAD','MUMBAI','MH','INDIA');
VALUES(1002,'JM ROAD','PUNE','MH','INDIA');
VALUES(1003,'DF ROAD','NASIK','MH','INDIA');
VALUES(1004,'KARVE ROAD','PUNE','MH','INDIA');
VALUES(1005,'MJ STREET','NEW YORK','NY','USA');
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
INTO
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
SALES
VALUES(1,'I001','B001',1001,100000,300);
VALUES(1,'I002','B004',1003,25000,500);
VALUES(2,'I001','B001',1004,150000,600);
VALUES(2,'I002','B003',1001,122000,250);
VALUES(3,'I001','B004',1005,150000,450);
VALUES(3,'I003','B001',1001,99000,1000);
VALUES(3,'I001','B001',1003,65000,200);
VALUES(3,'I001','B004',1001,10000,700);
VALUES(1,'I003','B003',1002,24000,550);
VALUES(2,'I001','B001',1001,100000,300);
VALUES(2,'I001','B002',1002,55000,200);
VALUES(1,'I004','B003',1005,46000,150);
VALUES(5,'I004','B005',1001,80000,900);
VALUES(6,'I003','B002',1002,55500,250);
VALUES(6,'I001','B001',1005,90000,100);
VALUES(8,'I003','B002',1002,75000,150);
VALUES(4,'I001','B002',1002,80000,100);
VALUES(7,'I003','B002',1003,55500,120);
VALUES(7,'I002','B001',1005,90000,160);
VALUES(8,'I003','B002',1002,75000,650);
Q2.
Display day wise item-wise total units sold
select s.time_key,s.item_key, sum(s.units_sold) from
sales s , time t, item i
where s.time_key= t.time_key and s.item_key = i.item_key
group by s.time_key,s.item_key;
TIME_KEY
ITEM_KEY SUM(S.UNITS_SOLD)
---------------------- -------- ---------------------2
I002
250
5
I004
900
8
I003
800
7
I003
120
1
I001
300
4
I001
100
6
I003
250
6
I001
100
3
I001
1350
1
I003
550
1
I002
500
2
I001
1100
3
I003
1000
1
I004
150
7
I002
160
15 rows selected
Q3 Display day wise item-wise total units sold in ascending order or timeid/key
select s.time_key,s.item_key, sum(s.units_sold) from
sales s , time t, item i
where s.time_key= t.time_key and s.item_key = i.item_key
group by s.time_key,s.item_key order by s.time_key;
TIME_KEY
ITEM_KEY SUM(S.UNITS_SOLD)
---------------------- -------- ---------------------1
I001
300
1
I002
500
1
I003
550
1
I004
150
2
I001
1100
2
I002
250
3
I001
1350
3
I003
1000
4
I001
100
5
I004
900
6
I001
100
6
I003
250
7
I002
160
7
I003
120
8
I003
800
15 rows selected
Q5. Display quarter wise units sold in ascending order of quarter number
select t.quarter, sum(s.units_sold) from
sales s , time t
where s.time_key = t.time_key
group by t.quarter order by t.quarter;
Analytical functions practical
Page 1 of 2
QUARTER SUM(S.UNITS_SOLD)
------- ---------------------Q1
5200
Q2
1800
Q3
280
Q4
350
4 rows selected
Q6. Display citywise and items brand wise total units sold
select l.city, i.brand, sum(s.units_sold)
from sales s , item i, location l
where s.location_key= l.location_key and s.item_key = i.item_key
group by l.city,i.brand order by l.city;
CITY
BRAND
SUM(S.UNITS_SOLD)
--------------- --------------- ---------------------MUMBAI
GODREJ
1550
MUMBAI
HL
900
MUMBAI
PnG
1000
NASIK
GODREJ
700
NASIK
PnG
120
NEW YORK
GODREJ
710
NEW YORK
HL
150
PUNE
GODREJ
900
PUNE
PnG
1600
9 rows selected
381.5
381.5
381.5
381.5
2
2
1
3
2
7
1
2
7
8
3
6
8
1
5
1
I001
I001
I001
I001
I001
I002
I002
I002
I003
I003
I003
I003
I003
I003
I004
I004
B002
B001
B001
B001
B001
B001
B004
B003
B002
B002
B001
B002
B002
B003
B005
B003
1002
1001
1001
1003
1004
1005
1003
1001
1003
1002
1001
1002
1002
1002
1001
1005
327.78
327.78
327.78
327.78
327.78
303.33
303.33
303.33
453.33
453.33
453.33
453.33
453.33
453.33
525
525
245
380
380
380
380
380
550
316.67
245
245
380
245
245
316.67
900
316.67
316.67
575
575
273.33
600
215
273.33
575
273.33
316.67
575
316.67
316.67
316.67
575
215
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
381.5
20 rows selected
Q10. Display the difference between each units_sold and branch avg
Select distinct b.branch_name , s.units_sold,avg(s.units_sold)
over(partition by b.branch_name) avg_units_sold , s.units_sold avg(s.units_sold) over(partition by b.branch_name) diff_avg
from branch b natural join sales s;
BRANCH_NAME
UNITS_SOLD
AVG_UNITS_SOLD
--------------- ---------------------- ---------------------- ---------------------NORTH MUMBAI 150
245
-95
NORTH MUMBAI 250
245
5
SOUTH MUMBAI 160
380
-220
SOUTH MUMBAI 300
380
-80
CENTRAL MUMBAI 900
462.5
437.5
NORTH MUMBAI 100
245
-145
NORTH MUMBAI 120
245
-125
CENTRAL MUMBAI 150
462.5
-312.5
SOUTH MUMBAI 100
380
-280
NORTH MUMBAI 200
245
-45
SOUTH MUMBAI 200
380
-180
SOUTH MUMBAI 1000
380
620
SOUTH MUMBAI 600
380
220
CENTRAL MUMBAI 550
462.5
87.5
CITY
500
550
-50
CITY
700
550
150
CENTRAL MUMBAI 250
462.5
-212.5
NORTH MUMBAI 650
245
405
CITY
450
550
-100
DIFF_AVG
19 rows selected
Q11. Display sales info along with number of items in branch and number of
items at location
Select time_key,item_key, branch_key, location_key , dollars_sold,
units_sold,
count(distinct item_key) over(partition by branch_key) branch_items,
count(distinct item_key) over(partition by location_key) location_items
from sales;
TIME_KEY ITEM_KEY BRANCH_KEY LOCATION_KEY DOLLARS_SOLD
UNITS_SOLD
BRANCH_ITEMS
LOCATION_ITEMS
---------------------- -------- ---------- ---------------------- ---------------------- ---------------------- ------------------------------------------1
I001
B001
1001
100000
300
3
4
2
I001
B001
1001
100000
300
3
4
6
I001
B001
1005
90000
100
3
3
2
I001
B001
1004
150000
600
3
1
3
I001
B001
1003
65000
200
3
3
7
I002
B001
1005
90000
160
3
3
3
I003
B001
1001
99000
1000
3
4
2
I001
B002
1002
55000
200
2
2
4
I001
B002
1002
80000
100
2
2
6
I003
B002
1002
55500
250
2
2
8
I003
B002
1002
75000
650
2
2
8
I003
B002
1002
75000
150
2
2
7
I003
B002
1003
55500
120
2
3
2
I002
B003
1001
122000
250
3
4
1
1
3
3
1
5
I003
I004
I001
I001
I002
I004
B003
B003
B004
B004
B004
B005
1002
1005
1001
1005
1003
1001
24000
46000
10000
150000
25000
80000
550
150
700
450
500
900
3
3
2
2
2
1
2
3
4
3
3
4
20 rows selected
LOCATION_LASTVALUE
---------------------1000
300
300
250
250
250
250
150
650
100
100
100
200
200
200
600
160
450
450
450
Q.15 Calculate the number of units between 100 less and 200 more than each
items units sold.
Select item_key, dollars_sold, units_sold, count(units_sold) over( order
by units_sold range between 100 preceding and 200 following)
num_of_units from sales;
ITEM_KEY DOLLARS_SOLD
UNITS_SOLD
NUM_OF_UNITS
-------- ---------------------- ---------------------- ---------------------I001
80000
100
12
I001
90000
100
12
I003
55500
120
12
I004
46000
150
12
I003
75000
150
12
I002
90000
160
12
I001
65000
200
12
I001
55000
200
12
I002
122000
250
10
I003
55500
250
10
I001
100000
300
8
I001
100000
300
8
I001
150000
450
5
I002
25000
500
6
I003
24000
550
6
I001
150000
600
5
I003
75000
650
4
I001
10000
700
4
I004
80000
900
2
I003
99000
1000
2
20 rows selected
Q16. Display the total number of items having more dollars_sold than current
item
Select item_key, dollars_sold, count(dollars_sold) over( order by
dollars_sold range between 1 following and unbounded following)
num_of_dollars_sold_more
from sales order by dollars_sold;
ITEM_KEY DOLLARS_SOLD
NUM_OF_DOLLARS_SOLD_MORE
-------- ---------------------- -----------------------I001
10000
19
I003
24000
18
I002
25000
17
I004
46000
16
I001
55000
15
I003
55500
13
I003
55500
13
I001
65000
12
I003
75000
10
I003
75000
10
I001
80000
8
I004
80000
8
I002
90000
6
I001
90000
6
I003
99000
5
I001
100000
3
I001
100000
3
I002
122000
2
I001
150000
0
I001
150000
0
20 rows selected
Q17. Display average dollars_sold item wise and its calculated from following
rows
Select item_key,location_key, dollars_sold,
avg(dollars_sold) over (partition by item_key order by location_key
rows between 1 following and unbounded following) avg_dollars_sold
from sales order by item_key;
Analytical functions practical
Page 1 of 2
ITEM_KEY LOCATION_KEY
DOLLARS_SOLD
AVG_DOLLARS_SOLD
-------- ---------------------- ---------------------- ---------------------I001
1001
10000
98750
I001
1001
100000
98571.4285714285714285714285714285714286
I001
1001
100000
98333.3333333333333333333333333333333333
I001
1002
80000
102000
I001
1002
55000
113750
I001
1003
65000
130000
I001
1004
150000
120000
I001
1005
90000
150000
I001
1005
150000
I002
1001
122000
57500
I002
1003
25000
90000
I002
1005
90000
I003
1001
99000
57000
I003
1002
55500
57375
I003
1002
75000
51500
I003
1002
75000
39750
I003
1002
24000
55500
I003
1003
55500
I004
1001
80000
46000
I004
1005
46000
20 rows selected