0% found this document useful (1 vote)
156 views

Module 2 Assignment: Query 1: Sales Order Shipments by Month and Category Code1

The document provides instructions for a module 2 assignment involving writing SQL SELECT statements using subtotal operators such as CUBE, ROLLUP, and GROUPING SETS. It includes 7 sample queries to write involving summarizing data from tables in an Inventory Data Warehouse by various columns and calculating totals. It asks students to rewrite two of the queries without using CUBE, ROLLUP, or GROUPING SETS operators.

Uploaded by

Thunder Bolt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
156 views

Module 2 Assignment: Query 1: Sales Order Shipments by Month and Category Code1

The document provides instructions for a module 2 assignment involving writing SQL SELECT statements using subtotal operators such as CUBE, ROLLUP, and GROUPING SETS. It includes 7 sample queries to write involving summarizing data from tables in an Inventory Data Warehouse by various columns and calculating totals. It asks students to rewrite two of the queries without using CUBE, ROLLUP, or GROUPING SETS operators.

Uploaded by

Thunder Bolt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

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

problems, you should follow the examples given in the notes.

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

rows or just recreate and repopulate the tables.

Query 1: Sales Order Shipments by Month and Category Code1

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 totals for every combination of grouped columns. Do
not use the GROUPING SETS and UNION operators.
select dd.CalMonth, cd.AddrCatCode1, sum(inf.ExtCost) as totals
from date_dim dd, inventory_fact inf, addr_cat_code1 ad1, cust_vendor_dim cd
where dd.DateKey = inf.DateKey and cd.AddrCatCode1 = ad1.AddrCatCodeKey and
inf.CustVendorKey = cd.CustVendorKey and dd.CalYear = '2011' and inf.TransTypeKey
= 5
group by cube (dd.calmonth, cd.AddrCatCode1)
order by dd.CalMonth, cd.AddrCatCode1

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

Query 2: Sales Order Shipments by Name, Zip, and Quarter

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))

Query 3: Transfers by Company and Branch Plant

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 name and branch plant name. The result should include the grouped columns and a
partial set of 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 operators.
select co.CompanyName, bd.BPName, sum(ExtCost) as totalExtCost, sum(Quantity)
as 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
group by rollup (co.CompanyName, bd.BPName)
order by co.CompanyName, bd.BPName

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

Query 4: Inventory Transactions by Transaction Description, Company,


and Branch Plant

Write an SQL statement to display the sum of the extended cost and the number of inventory
transactions. The results should include data for all transaction types. Summarize the result by
transaction description, company name, and branch plant name. The result should include the
grouped columns and partial totals in order of the grouped columns (transaction description,
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 where co.CompanyKey = bd.CompanyKey and bd.BranchPlantKey =
inf.BranchPlantKey and tr.TransTypeKey = inf.TransTypeKey
group by grouping sets ((co.CompanyName , bd.BPName, tr.TransDescription),
(co.CompanyName, tr.TransDescription), (tr.TransDescription),())

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

Query 5: Adjustments by Part Number

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

Query 6: Rewrite Query 1 without CUBE, ROLLUP, or GROUPING SETS

Rewrite query 1 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.
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
where A.datekey = B.datekey and A.custvendorkey = c.custvendorkey
and transtypekey = 5 and B.calyear = 2011
group by B.calmonth, c.addrcatcode1
union
select B.calmonth, 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
group by B.calmonth
union
select null, c.addrcatcode1,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

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

Query 7: Rewrite Query 3 without CUBE, ROLLUP, or GROUPING SETS

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.
select c.companyname, B.bpname,sum(extcost) as total_cost, sum(quantity)
as total_quantity
from inventory_fact A , branch_plant_dim B , company_dim c
where A.BranchPlantKey=B.BranchPlantKey and c.companykey = B.companykey
and transtypekey = 2
group by c.companyname, B.bpname
union
select c.companyname, null,sum(extcost), sum (quantity) from
inventory_fact A , branch_plant_dim B , company_dim c
where A.BranchPlantKey=B.BranchPlantKey and c.companykey = B.companykey
and transtypekey = 2
group by c.companyname
union
select null, null,sum(extcost), sum(quantity)
from inventory_fact A , branch_plant_dim B , company_dim c
where A.BranchPlantKey=B.BranchPlantKey and c.companykey = B.companykey
and transtypekey = 2
order by 1,2;

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

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 include the grouped columns and the full set of subtotals for customer name
and the combination of year and quarter. Do not use the GROUPING SETS and UNION
operators. (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
where A.datekey = B.datekey and A.custvendorkey = c.custvendorkey and
transtypekey = 5 and B.calyear in (2011,2012)
group by cube ( c.name, (B.calyear,B.calquarter))
order by B.calyear,B.calquarter, c.name

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

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
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 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).
select calyear, calquarter, name, sum(extcost) as total_cost,
count(inventorykey) 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)
order by calyear, calquarter, name

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

Grading
Your performance will be assessed by a quiz designed to test your understanding of each
problem and by evidence of query executions. Since some quiz questions involve execution results,
you should execute your statements using the original inventory data warehouse tables.

You will receive 50% if your documentation contains a SELECT statement and partial

results for each problem. Execution of your SQL statements demonstrates correct syntax.

The quiz score will provide the other 50% of your grade. The quiz contains
questions about the important elements of each problem such as the usage of the
correct tables, function, partitioning, and sorting.

Completion
You should upload a file containing your Oracle SQL statements and execution results to

the graded item in module 2. For the execution results, you should take a snapshot of the script

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 statement.

This study source was downloaded by 100000821744388 from CourseHero.com on 05-14-2021 04:56:04 GMT -05:00

Powered by TCPDF (www.tcpdf.org)


https://www.coursehero.com/file/75492102/15-Ha-Hai-Long-Lab7doc/

You might also like