This Study Resource Was: Module 2 Assignment
This Study Resource Was: Module 2 Assignment
The Module 2 Assignment provides experience writing SELECT statements using the
subtotal operators (CUBE, ROLLUP, and GROUPING SETS). For the subtotal operator
Your SELECT statements will reference the tables of the Inventory Data Warehouse,
described in another document. The INSERT statements are provided in another document. The
Inventory Data Warehouse design and rows are identical from module 5 in course 2. If you added
rows through the data integration assignment in module 5 of course 2, you should remove those
m
rows or just recreate and repopulate the tables.
er as
co
eH w
Query 1: Sales Order Shipments by Month and Category Code1
o.
rs e
Write an SQL statement to display the sum of the extended cost and the sum of the quantity. The
ou urc
results should include data for shipments (transaction type 5) in calendar year 2011. Summarize
the result by calendar month and Address Category Code 1. The result should include the
grouped columns and the full totals for every combination of grouped columns. Do not use the
o
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 2
Write an SQL statement to display the sum of the extended cost and the number of inventory
transactions. The results should include data for shipments (transaction type 5) in calendar years
2011 and 2012. Summarize the result by calendar quarter, customer zip code, and customer
name. The result should include the grouped columns and full set of subtotals for every
combination of grouped columns. Do not use the CUBE and UNION operators.
select cd.Name, cd.Zip, dd.CalQuarter, sum(inf.ExtCost) totalExtcost,
sum(inf.TransTypeKey) as totalTrans
from date_dim dd, inventory_fact inf, cust_vendor_dim cd
where dd.DateKey = inf.DateKey and inf.CustVendorKey = cd.CustVendorKey and
(dd.CalYear = '2011' or dd.CalYear = '2012') and inf.TransTypeKey = 5
group by grouping sets (cd.Name, cd.Zip, dd.CalQuarter,(dd.CalQuarter, cd.Zip))
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
Write an SQL statement to display the sum of the extended cost and the sum of the quantity. The
results should include data for transfers (transaction type 2). Summarize the result by company
ed d
name and branch plant name. The result should include the grouped columns and a partial set of
ar stu
subtotals in order of the grouped columns (company name and branch plant name). Transfer
quantities by design should sum to zero across all companies so that the grand total should be 0
for the sum of quantity and extended cost. Do not use the GROUPING SETS and UNION
is
operators.
select co.CompanyName, bd.BPName, sum(ExtCost) as totalExtCost, sum(Quantity) as
Th
totalQuantity
from company_dim co, branch_plant_dim bd, inventory_fact inf
where co.CompanyKey = bd.CompanyKey and bd.BranchPlantKey = inf.BranchPlantKey
and inf.TransTypeKey = 2
sh
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 3
m
Branch Plant
er as
co
Write an SQL statement to display the sum of the extended cost and the number of inventory
eH w
transactions. The results should include data for all transaction types. Summarize the result by
o.
transaction description, company name, and branch plant name. The result should include the
rs e
grouped columns and partial totals in order of the grouped columns (transaction description,
ou urc
company name, and branch plant name). Do not use the ROLLUP and UNION operators.
select co.CompanyName, bd.BPName, tr.TransDescription, sum(ExtCost) as
totalExtCost, sum(inf.TransTypeKey) as totalTrans
from company_dim co, branch_plant_dim bd, inventory_fact inf, trans_type_dim tr
o
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 4
Write an SQL statement to display the sum of the extended cost and the number of inventory
transactions. The results should include data for shipments (transaction type 5) in calendar years
2011 and 2012. Summarize the result by calendar year, calendar quarter, and customer name. The
result should show the grouped columns and the normal set of group by results plus partial
subtotals for year and quarter concatenated with customer name. Do not use the GROUPING
SETS and UNION operators. (Hint: see the partial ROLLUP example in lesson 5).
select dd.CalYear, dd.CalQuarter, cd.Name, sum(ExtCost) as totalExtCost,
sum(inf.TransTypeKey) as totalTrans
from date_dim dd, inventory_fact inf, cust_vendor_dim cd
where dd.DateKey = inf.DateKey and inf.CustVendorKey = cd.CustVendorKey and
dd.CalYear in (2011,2012) and TransTypeKey = 5
group by rollup (dd.CalYear, dd.CalQuarter), cd.Name
order by dd.CalYear, dd.CalQuarter, cd.Name
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
Rewrite query 1 without the usage of the CUBE ROLLUP, or GROUPING SETS operators. In
ar stu
rewriting the query, you should use NULL as the default value for each column.
select B.calmonth, c.addrcatcode1,sum(extcost) as total_cost, sum(quantity) as
total_quantity
from inventory_fact A , date_dim B ,cust_vendor_dim c
is
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 5
group by c.addrcatcode1
union
select null, null,sum(extcost), sum(quantity)
from inventory_fact A , date_dim B ,cust_vendor_dim c
where A.datekey = B.datekey and A.custvendorkey = c.custvendorkey and
transtypekey = 5 and B.calyear = 2011
order by 1,2
m
er as
co
eH w
o.
rs e
Query 7: Rewrite Query 3 without CUBE, ROLLUP, or GROUPING SETS
ou urc
Rewrite query 3 without the usage of the CUBE, ROLLUP, or GROUPING SETS operators. In
rewriting the query, you should use NULL as the default value for each column.
o
total_quantity
from inventory_fact A , branch_plant_dim B , company_dim c
vi y re
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 6
Query 8: Sales Order Shipments by Name and Combination of Year and Quarter
m
er as
Write an SQL statement to display the sum of the extended cost and the number of inventory
co
transactions. The results should include data for shipments (transaction type 5) in calendar years
eH w
2011 and 2012. Summarize the result by calendar year, calendar quarter, and customer name. The
o.
result should include the grouped columns and the full set of subtotals for customer name and the
rs e
combination of year and quarter. Do not use the GROUPING SETS and UNION operators.
ou urc
(Hint: see the composite column example in lesson 5).
select B.calyear,B.calquarter, c.name, sum(extcost) as total_cost,
count(inventorykey) as total_transactions
from inventory_fact A , date_dim B , cust_vendor_dim c
o
Query 9: Sales Order Shipments by Month and Category Code1 with Group
Number
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 7
Write an SQL statement to display the sum of the extended cost and the sum of the quantity. The
results should include data for shipments (transaction type 5) in calendar year 2011. Summarize
the result by calendar month and Address Category Code 1. The result should include the
grouped columns and the full set of subtotals for every combination of grouped columns along
with the hierarchical group number for both grouping columns. Do not use the GROUPING
SETS and UNION operators. (Hint: see the group functions slide in lesson 5).
select CalMonth,AddrCatCode1,sum(ExtCost) as total_cost,sum(Quantity) as
total_quantity ,GROUPING_ID(CalMonth,AddrCatCode1)as group_level
from inventory_fact A , date_dim B , cust_vendor_dim C
where A.DateKey=B.DateKey and A.CustVendorKey=C.CustVendorKey
and TransTypeKey=5 and B.CalYear=2011
group by cube(CalMonth,AddrCatCode1)
order by CalMonth,AddrCatCode1
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
Query 10: Sales Order Shipments with Subtotals by Name and Partial Subtotals
by Year and Quarter
Write an SQL statement to display the sum of the extended cost and the number of inventory
ed d
transactions. The results should include data for shipments (transaction type 5) in calendar years
ar stu
2011 and 2012. Summarize the result by calendar year, calendar quarter, and customer name. The
result should include the grouped columns and subtotals for customer name along with partial
subtotals for year and quarter. Do not include the normal GROUP BY totals in the result. Do not
use the UNION operator. (Hint: see the nested rollup example in lesson 5).
is
as total_transactions
from inventory_fact A , date_dim B ,cust_vendor_dim c
where A.datekey = B.datekey and A.custvendorkey = c.custvendorkey and
transtypekey = 5 and B.calyear in (2011,2012)
group by grouping sets (rollup(calyear, calquarter), name)
sh
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
12/7/2020 Module 2 Assignment Page 8
m
er as
Grading
co
eH w
Your performance will be assessed by a quiz designed to test your understanding of each
o.
problem and by evidence of query executions. Since some quiz questions involve execution
rs e
results, you should execute your statements using the original inventory data warehouse tables.
ou urc
You will receive 50% if your documentation contains a SELECT statement and partial
o
results for each problem. Execution of your SQL statements demonstrates correct syntax.
aC s
The quiz score will provide the other 50% of your grade. The quiz contains questions
vi y re
about the important elements of each problem such as the usage of the correct tables,
function, partitioning, and sorting.
ed d
ar stu
Completion
You should upload a file containing your Oracle SQL statements and execution results to
is
the graded item in module 2. For the execution results, you should take a snapshot of the script
Th
output window in SQL Developer showing the execution results. You only need to show the first
10 rows or so of the result. You should paste the execution results after the associated SELECT
sh
statement.
This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00
https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/
Powered by TCPDF (www.tcpdf.org)