Lab-2_asgmt-2 (1)
Lab-2_asgmt-2 (1)
29-Jan-2025
Processing
Before running the below query, follow the steps provided in the image
The column name should be the same
COPY nation
FROM 'Path\\tpc-h\\nation.tbl'
WITH (
FORMAT CSV,
DELIMITER '|',
HEADER TRUE
);
Q2 select 100 minimum cost select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address,
supplier from part, partsupp, s_phone, s_comment from PART, SUPPLIER, PARTSUPP,
supplier, nation, region where NATION, REGION where p_partkey = ps_partkey and
name= ‘aisa’ s_suppkey = ps_suppkey and p_size = 30 and p_type like
'%STEEL' and s_nationkey = n_nationkey and n_regionkey =
r_regionkey and r_name = 'ASIA' and ps_supplycost = (select
min(ps_supplycost) from PARTSUPP, SUPPLIER, NATION,
REGION where p_partkey = ps_partkey and s_suppkey =
ps_suppkey and s_nationkey = n_nationkey and n_regionkey =
r_regionkey and r_name = 'ASIA') order by s_acctbal desc,
n_name, s_name, p_partkey limit 100;
Q4 select order priority from select o_orderpriority, count(*) as order_count from ORDERS
orders where o_orderdate >= where o_orderdate >= date '1995-01-01' and o_orderdate < date
date '1995-01-01' and '1995-01-01' + interval '3' month and exists (select * from
o_orderdate < date '1995-01- LINEITEM where l_orderkey = o_orderkey and l_commitdate <
01' + interval '3' month l_receiptdate) group by o_orderpriority order by o_orderpriority;
Q7 select volume shipping from select supp_nation, cust_nation, l_year, sum(volume) as revenue
supplier, lineitem, orders, from ( select n1.n_name as supp_nation, n2.n_name as
customer, nation where cust_nation, extract(year from l_shipdate) as l_year,
n1.name = 'JAPAN' and l_extendedprice * (1 - l_discount) as volume from SUPPLIER,
n2.name = 'INDIA' or LINEITEM, ORDERS, CUSTOMER, NATION n1, NATION n2
n1.name = ‘INDIA’ and where s_suppkey = l_suppkey and o_orderkey = l_orderkey and
n2.name = ‘JAPAN’ and c_custkey = o_custkey and s_nationkey = n1.n_nationkey and
l_shipdate between date c_nationkey = n2.n_nationkey and ((n1.n_name = 'JAPAN' and
'1995-01-01' and date '1996- n2.n_name = 'INDIA') or (n1.n_name = 'INDIA' and n2.n_name =
12-31') 'JAPAN')) and l_shipdate between date '1995-01-01' and date
'1996-12-31') as shipping group by supp_nation, cust_nation,
l_year order by supp_nation, cust_nation, l_year;
Q8 select national market share select o_year, sum(case when nation = 'INDIA' then volume else
from from part, supplier, 0 end) / sum(volume) as mkt_share from (select extract(year from
lineitem, orders, customer, o_orderdate) as o_year, l_extendedprice * (1 - l_discount) as
nation n1, nation n2, region volume, n2.n_name as nation from PART, SUPPLIER,
where o_orderdate between LINEITEM, ORDERS, CUSTOMER, NATION n1, NATION n2,
date '1995-01-01' and date REGION where p_partkey = l_partkey and s_suppkey =
'1996-12-31'and p_type = l_suppkey and l_orderkey = o_orderkey and o_custkey =
'SMALL PLATED COPPER' c_custkey and c_nationkey = n1.n_nationkey and n1.n_regionkey
= r_regionkey and r_name = 'ASIA' and s_nationkey =
n2.n_nationkey and o_orderdate between date '1995-01-01' and
date '1996-12-31'and p_type = 'SMALL PLATED COPPER') as
all_nations group by o_year order by o_year;
Q9 select product type profit select nation, o_year, sum(amount) as sum_profit from (select
measure from part, supplier, n_name as nation, extract(year from o_orderdate) as o_year,
lineitem, partsupp, orders, l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as
nation name like '%dim%' amount from PART, SUPPLIER, LINEITEM, PARTSUPP,
ORDERS, NATION where s_suppkey = l_suppkey and
ps_suppkey = l_suppkey and ps_partkey = l_partkey and
p_partkey = l_partkey and o_orderkey = l_orderkey and
s_nationkey = n_nationkey and p_name like '%dim%') as profit
group by nation, o_year order by nation, o_year desc;
Q12 select shipping modes and select l_shipmode, sum(case when o_orderpriority = '1-URGENT'
order priority rom orders, or o_orderpriority = '2-HIGH' then 1 else 0 end) as
lineitem where l_shipmode in high_line_count, sum(case when o_orderpriority <> '1-URGENT'
('RAIL', 'FOB') and and o_orderpriority <> '2-HIGH' then 1 else 0 end) as
l_commitdate < l_receiptdate low_line_count from ORDERS, LINEITEM where o_orderkey =
and l_shipdate < l_orderkey and l_shipmode in ('RAIL', 'FOB') and l_commitdate
l_commitdate and < l_receiptdate and l_shipdate < l_commitdate and
l_receiptdate >= date '1997- l_receiptdate >= date '1997-01-01' and l_receiptdate < date '1997-
01-01' and l_receiptdate < 01-01' + interval '1' year group by l_shipmode order by
date '1997-01-01' + interval '1' l_shipmode;
year
Q13 select customer distribution in select c_count, count(*) as custdist from (select c_custkey,
descending order from count(o_orderkey) as c_count from CUSTOMER left outer join
customer left outer join orders ORDERS on c_custkey = o_custkey and o_comment not like
on c_custkey = o_custkey and '%pending%deposits%' group by c_custkey) c_orders group by
o_comment not like c_count order by custdist desc, c_count desc;
'%pending%deposits%'
Q14 select promotion effect from select 100.00 * sum(case when p_type like 'PROMO%' then
linitem, part where l_extendedprice * (1 - l_discount) else 0 end) /
l_shipdate >= date '1996-12- sum(l_extendedprice * (1 - l_discount)) as promo_revenue from
01' and l_shipdate < date LINEITEM, PART where l_partkey = p_partkey and
'1996-12-01' + interval '1' l_shipdate >= date '1996-12-01' and l_shipdate < date '1996-12-
month 01' + interval '1' month;
Q15 create view of top supplier create view REVENUE0 (supplier_no, total_revenue) as select
from lineitem where l_suppkey, sum(l_extendedprice * (1 - l_discount)) from
l_shipdate >= date '1997-07- LINEITEM where l_shipdate >= date '1997-07-01' and l_shipdate
01' and l_shipdate < date < date '1997-07-01' + interval '3' month group by l_suppkey;
'1997-07-01' + interval '3' select s_suppkey, s_name, s_address, s_phone, total_revenue
month from SUPPLIER, REVENUE0 where s_suppkey = supplier_no
and total_revenue = ( select max(total_revenue) from
REVENUE0) order by s_suppkey; drop view REVENUE0;
Q18 select 100 large volume select c_name, c_custkey, o_orderkey, o_orderdate, o_totalprice,
customer from CUSTOMER, sum(l_quantity) from CUSTOMER, ORDERS, LINEITEM
ORDERS, LINEITEM where where o_orderkey in (select l_orderkey from LINEITEM group
sum(l_quantity) > 314) by l_orderkey having sum(l_quantity) > 314) and c_custkey =
o_custkey and o_orderkey = l_orderkey group by c_name,
c_custkey, o_orderkey, o_orderdate, o_totalprice order by
o_totalprice desc, o_orderdate limit 100;
Q20 select potential part promotion select s_name, s_address from SUPPLIER, NATION where
from supplier, nation where s_suppkey in ( select ps_suppkey from PARTSUPP where
s_suppkey in ( select ps_partkey in (select p_partkey from PART where p_name like
ps_suppkey from partsupp 'green%') and ps_availqty > (select 0.5 * sum(l_quantity) from
where ps_partkey in (select LINEITEM where l_partkey = ps_partkey and l_suppkey =
p_partkey from part where ps_suppkey and l_shipdate >= date '1993-01-01' and l_shipdate <
p_name like 'green%') and date '1993-01-01' + interval '1' year)) and s_nationkey =
ps_availqty > (select 0.5 * n_nationkey and n_name = 'ALGERIA' order by s_name;
sum(l_quantity) from lineitem
where l_shipdate >= date
'1993-01-01' and l_shipdate <
date '1993-01-01' + interval '1'
year)) and n_name =
'ALGERIA'
Q21 select 100 suppliers who kept select s_name, count(*) as numwait from SUPPLIER, LINEITEM
orders waiting from supplier, l1, ORDERS, NATION where s_suppkey = l1.l_suppkey and
lineitem l1, orders, nation o_orderkey = l1.l_orderkey and o_orderstatus = 'F' and
where o_orderstatus = 'F' and l1.l_receiptdate > l1.l_commitdate and exists ( select * from
l1.l_receiptdate > LINEITEM l2 where l2.l_orderkey = l1.l_orderkey and
l1.l_commitdate and exists l2.l_suppkey <> l1.l_suppkey) and not exists (select * from
( select * from lineitem not LINEITEM l3 where l3.l_orderkey = l1.l_orderkey and
exists (select * from lineitem l3.l_suppkey <> l1.l_suppkey and l3.l_receiptdate >
n_name = 'EGYPT' l3.l_commitdate) and s_nationkey = n_nationkey and n_name =
'EGYPT' group by s_name order by numwait desc, s_name limit
100;
Q22 select global sales opportunity select cntrycode, count(*) as numcust, sum(c_acctbal) as
from (select totacctbal from (select substring(c_phone from 1 for 2) as
substring(c_phone from 1 for cntrycode, c_acctbal from CUSTOMER where substring(c_phone
2) as cntrycode, c_acctbal from 1 for 2) in ('20', '40', '22', '30', '39', '42', '21') and c_acctbal >
from customer where ( select avg(c_acctbal) from CUSTOMER where c_acctbal > 0.00
substring(c_phone from 1 for and substring(c_phone from 1 for 2) in ('20', '40', '22', '30', '39',
2) in ('20', '40', '22', '30', '39', '42', '21')) and not exists ( select * from ORDERS where
'42', '21') and c_acctbal > o_custkey = c_custkey)) as custsale group by cntrycode order by
( select avg(c_acctbal) from cntrycode;
customer where c_acctbal >
0.00 and substring(c_phone
from 1 for 2) in ('20', '40', '22',
'30', '39', '42', '21')) and not
exists ( select * from
ORDERS where o_custkey =
c_custkey)) as custsale group
by cntrycode order by
cntrycode;