Assignment 3
Creating and Manipulating Materialized Views
Due: 3/9/2005 (10%)
Assignment 3 provides experience creating and manipulating materialized views in
Oracle. You will use several new SQL statements along with variations of the GROUP BY clause
for data warehouse queries. Sections 1 and 2 are background for the tasks described in Sections 3
through 5.
1. Inventory Transaction Cycles
Inventory that is bought, sold, consumed, and produced is the heart of any manufacturing
and/or distribution company. Inventory transactions are frequent and commonplace. The
volume and significance of these transactions make them ideally suited for OLAP via a data
warehouse.
Because inventory management is a common and important yet difficult activity in many
organizations, ERP vendors have developed Enterprise Resource Management (ERP) software to
provide software support. Typically, ERP software provides modules related to Manufacturing,
Distribution/Logistics, Financials, and HR/Payroll. At the heart of the Manufacturing and
Distribution/Logistics modules is inventory. The work order, sales, and purchase life cycles
affect the perpetual inventory balance as shown in Figure 1. In addition, inventory transactions
including adjustments, transfers, issues, and reclassifications affect the perpetual inventory
balance.
7/9/2018 Assignment 3 (Materialized Views) Page 2
Figure 1: Life Cycles Affecting the Perpetual Inventory Balance
2. Snowflake Schema Description
To support reporting about inventory management, Figure 2 shows a snowflake schema for
the perpetual inventory balance. The snowflake schema provides a template that can be
customized to individual organizations. Dimension entity types such as Addr_Cat_Code1 allow
an organization to customize the design to specific requirements. The fact entity type,
Inventory_Fact, contains several measures along with relationships to associated dimension
entity types. Several dimension entity types are related directly to the Inventory_Fact entity type.
Other dimension entity types such as Item_Cat_Code1 are indirectly related to the
Inventory_Fact entity type.
7/9/2018 Assignment 3 (Materialized Views) Page 3
date_dim inventory_fact
Summary_by_Day
DateKey InventoryKey trans_type_dim
Inventory_Transaction
CalDay zip_codes BranchPlantKey TranTypeKey
CalMonth ZipKey CustVendorKey TransDescription
CalQuarter ZipCity DateKey TransTypeCodeId
CalYear ExtCost Inventory_Facility
ZipConcec
DateJulian ZipState ItemMasterKey
DayofWeek ZipWeight Quantity
FicalYear TransTypeKey branch_plant_dim
ZipZip
FiscalPeriod UnitCost BranchPlantKey
defines Related_Address_Book BPName
Part_Number_ BranchPlantId
addr_cat_code1 item _cat_code1 CarryingCost
CompanyKey
AddrCatCodeKey ItemCatCodeKey
CostMethod
cust_vendor_dim AddrCatCodeId ItemCatCodeId
AddrCatDesc ItemCatDesc
CustVendorKey item _m aster_dim
AddrBookId Ow ning_Company
ItemMasterKey
AddrCatCode1 Addr_Code1 Item_Code1 ItemCatCode1
AddrCatCode2 ItemCatCode2
Addr_Code2 Item_Code2
Address ItemDesc com pany_dim
City SecondItemId CompanyKey
Country ShortItemId
addr_cat_code2 item _cat_code2 CompanyId
Name ThirdItemId
AddrCatCodeKey ItemCatCodeKey CompanyName
PrimZip UOM
AddrCatCodeId ItemCatCodeId CurrencyCode
State
AddrCatDesc ItemCatDesc CurrencyDesc
Zip
Figure 2: Generic Snowflake Schema for the Perpetual Inventory Balance
The ERD representation of the snowflake schema is converted to a table design using the
normal conversion rules. In the conversion process, 1-M relationships convert to foreign keys in
the child tables. Appendix A contains a data dictionary for the table design while Appendix B
contains the CREATE TABLE statements. Denormalization is always a good option to consider
for a snowflake schema implementation. However, the snowflake schema design reflected in
Appendices A and B is normalized.
To populate your tables, you can access my tables on the COBCU2 server. You can use the
INSERT … SELECT statement to populate your tables using the data in my tables. Because you
have SELECT access to my tables, you can use my tables instead of your tables for the
assignment details in sections 4 and 5 if you wish. You can access my tables by using my user
name (mmannino) as a prefix to the table names of the inventory data warehouse. For example to
reference the inventory fact table, you should use “mmannino.Inventory_Fact”. For CREATE
7/9/2018 Assignment 3 (Materialized Views) Page 4
DIMENSION statements in section 3, you need to reference your own tables. Thus you will need
to create the inventory data warehouse tables but you do not need to populate them.
3. Create Dimensions
Write Oracle CREATE DIMENSION statements to create the dimensions of the snowflake
schema tables as specified in the following lists. In writing the CREATE DIMENSION
statements, you should use the JOIN KEY clause if the dimension hierarchy contains levels from
more than one table. You should use the DETERMINES clause to provide dimension constraints.
To assist in my grading, please include DROP DIMENSION statements before your CREATE
DIMENSION statements.
Create a dimension for customer vendor data. The dimension should include levels
for the customer vendor key, the vendor city, the vendor state, the vendor zip, the
vendor country, the address category code key of the first address code, and the
address category code key of the second address code. Define hierarchies to show the
relationships between the customer vendor key and the vendor’s location, the
customer vendor key and the first address code, and the customer vendor key and the
second address code. Use the ATTRIBUTE clause to add functional relationships to
columns in the Address Category Code 1 table and the Address Category Code 2
table.
Create a dimension for item master data. The dimension should include levels for the
item master key, the item category 1 code, and the item category 2 code. Define
hierarchies to show relationships between the item master key and the item category 1
code and the item master key and the item category 2 code. Use the ATTRIBUTE
7/9/2018 Assignment 3 (Materialized Views) Page 5
clause to add functional relationships to columns in the item category 1 and item
category 2 tables.
Create a dimension for branch-plant data. The dimension should include levels for
the branch plant key and the company key. Define a hierarchy to show the
relationship between the branch plant key and the company key. Use the
ATTRIBUTE clause to add functional relationships to columns in the branch-plant
and company tables.
Create a dimension for date data. The dimension should include levels for the date
key, the calendar day, the calendar month, the calendar quarter, the calendar year, and
the day of the week. Define hierarchies to show relationships among the date key, the
calendar day, the calendar month, the calendar quarter, and the calendar year, and
between the date key and the day of the week. Use the ATTRIBUTE clause to add
functional relationships to the Julian date column in the date dimension table.
4. Write Queries
Write Oracle SELECT statements for the following problems. You may need to use the
CUBE and ROLLUP clauses in your SELECT statements. Your SELECT statements should
reference the fact table (Inventory_Fact) along with the dimension tables of the snowflake
schema in Appendix B. Your SELECT statements should not reference the dimensions that you
created in part 3. Use the CUBE and ROLLUP options of the GROUP BY clause where
indicated in the problem statements.
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 the year 2005. Summarize the
result by calendar month and Address Category Code 1. The result should include full totals for
every combination of grouped fields.
7/9/2018 Assignment 3 (Materialized Views) Page 6
Query 2: Sales Order Shipments by Name, 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 the years 2005
and 2006. Summarize the result by calendar year, calendar quarter, and customer name. The
result should include full totals for every combination of grouped fields.
Query 3: Adjustments by Part Number
Write an SQL statement to display the sum of the extended cost, the sum of the quantity, and the
number of inventory transactions. The results should include data for adjustments (transaction
type 1). Summarize the result by Second Item Id (i.e. part number). The result should include full
totals for every combination of grouped fields. In addition, sort the result by Second Item Id.
Query 4: Transfers by Part Number 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 Second
Item Id (i.e. part number) and branch plant name. The result should include partial totals in order
of the grouped fields (second item id and branch plant name). Transfer quantities by design
should sum to zero.
Query 5: Inventory Transactions by Transaction Description and Company
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 and company name. The result should include partial totals in order of the
grouped fields (transaction description and company name).
Query 6: Rewrite Queries without CUBE, ROLLUP, and GROUPING SETS
Rewrite queries 1 and 4 without the usage of the CUBE, ROLLUP, or GROUPING SETS
operators of the GROUP BY clause. The rewritten queries should still access the base tables, not
the materialized views specified in section 5. In rewriting the queries, you may use '' (two single
quotes) as the default text value and 0 as the default numeric value.
5. Create Materialized View and Rewrite Queries
Write CREATE MATERIALIZED VIEW statements according to the following
specifications. After you write the statements, rewrite queries 1 and 2 from Section 4 using the
materialized views. To assist in my grading, please include DROP MATERIALIZED VIEW
statements before your CREATE MATERIALIZED VIEW statements.
7/9/2018 Assignment 3 (Materialized Views) Page 7
Both materialized views should contain the sum of the extended cost, the sum of
the quantity, and the number of inventory transactions.
In both materialized views, these calculated amounts should be summarized by
the customer vendor key and the date key.
The first materialized view should include only sales shipment transactions
(transaction type 5) for the year 2005.
The second materialized view should include only sales shipment transactions
(transaction type 5) for the year 2006.
Neither materialized view should include subtotals that are created by the CUBE
and the ROLLUP keywords.
Rewrite queries 1 and 2 using the materialized views in place of the fact table.
Grading
If you follow the instructions, you should receive full credit. Here are my grading
guidelines:
The CREATE DIMENSION statements in Section 3 are worth 30%. Please include
DROP DIMENSION statements in your assignment listing.
The SELECT statements in Section 4 are worth 40%.
The CREATE MATERIALIZED VIEW statement and rewritten SELECT statements in
Section 5 are worth 30%. Please include DROP MATERIALIZED VIEW statements in
your assignment listing.
In the deduction of points, I will distinguish between major errors such as syntax errors
and missing statements, medium errors such as missing a table in a SELECT statement,
and minor errors such as missing a WHERE condition in a SELECT statement.
7/9/2018 Assignment 3 (Materialized Views) Page 8
Completion
Upload a file containing your Oracle SQL statements to the Digital Drop Box part of the
Blackboard website. Use the following naming scheme for your assignment file:
LastNameFirstNameA3. To facilitate grading, please format your statements neatly. To test your
code, I will execute your SQL statements using the tables in my account on the COBCU2 server.
Please indicate your name and Oracle user name in your document.
7/9/2018 Assignment 3 (Materialized Views) Page 9
Appendix A: Data Dictionary for the Snowflake Schema Table Design
Appendix A contains a brief description of each column in the tables of the Inventory
Snowflake Schema. The primary keys of most tables are based on Oracle sequences that are
defined in Appendix B. A number of columns are based on the Oracle One World specifications.
Address Category 1 Table (addr_cat_code1)
This table defines address category codes related to customers/vendors. These codes allow
customers/vendors to be group. Example grouping might be customer type, customer area, etc.
AddrCatCodeKey Unique Key based on sequence addr_cat_code1_seq.
AddrCatCodeId Four character category code
AddrCatDesc Thirty character category code description
Address Category 2 Table (addr_cat_code2)
This table defines address category codes related to customers/vendors. These codes allow
customers/vendors to be group. Example grouping might be customer type, customer area, etc.
AddrCatCodeKey Primary Key based on sequence addr_cat_code2_seq.
AddrCatCodeId Four character category code
AddrCatDesc Thirty character category code description
Item Category 1 Table (item_cat_code1)
This table defines item master category codes related to item masters (parts) These codes allow
part numbers to be group. Example grouping might be product class, spare part, finish good, etc.
ItemCatCodeKey Primary Key based on sequence item_cat_code1_seq.
ItemCatCodeId Four character category code
ItemCatDesc Thirty character category code description
Item Category 2 Table (item_cat_code2)
This table defines item master category codes related to item masters (parts) These codes allow
part numbers to be group. Example grouping might be product class, spare part, finish good, etc.
ItemCatCodeKey Primary Key based on sequence item_cat_code2_seq.
ItemCatCodeId Four character category code
ItemCatDesc Thirty character category code description
7/9/2018 Assignment 3 (Materialized Views) Page 10
Zip Codes Table (zip_codes)
This table provides the basis to create many unique customer records for a variety with a variety
of zip codes.
ZipKey Primary Key, user defined.
ZipCity City related to zip code
ZipState State related to zip code
ZipZip Zip Code
ZipConsec The zip code plus this number define the range of zip codes for this city
ZipWeight The weight (percentage * 100) that will be applied to creating customers. All
ZipWeight columns totaled should equal 100.
Date Sequence Table (date_dim)
This table provides the date pattern. Date patterns can be daily, five days per week, weekly or
monthly.
DateKey Primary Key based on sequence date_seq.
DateJulian Julian date in the form of YYYYDDD. Where YYYY is the year and DDD is the
sequential date.
CalDay Calendar day from 1 to 31.
CalMonth Calendar month from 1 to 12
CalQuarter Calendar quarter from 1 to 4
CalYear Calendar year valid for ranges from 1900 to 2100
DayOfWeek Day of the week, 1 to 7, 1 is Sunday, 2 is Monday, etc
FiscalYear Corresponding Fiscal Year
FiscalPeriod Corresponding Fiscal Period
Transaction Type Table (trans_type_dim)
This table defines the various types of inventory transactions. Examples include transfers,
adjustments, shipments, receipts, etc.
TransTypeKey Primary Key, hard coded to the following values.
TransTypeId =1 then inventory adjustment (IA)
TransTypeId =2 then inventory transfer (IT)
TransTypeId =3 then inventory simple issue (IS)
TransTypeId =4 then purchase order receipt (OV)
TransTypeId =5 then sales order shipment (AR)
TransTypeId =6 then mfg issue (IM)
TransTypeId =7 then mfg completion (IC)
TransTypeId =8 then mfg parent scrap (IS)
TransTypeId =9 then mfg component scrap (IZ)
TransTypeCodeId Corresponding JDE cardex code, an example is IA for an adjustment.
TransDescription Transaction Type Description
7/9/2018 Assignment 3 (Materialized Views) Page 11
Customer Vendor Table (cust_vendor_dim)
This table defines possible customers and vendors involved with related sales and purchasing
related transactions.
CustVendorKey Primary Key based on the sequence cust_vendor_seq.
AddrBookId The JDE related address book number.
Name Customer Name
Address Address
City
State
PrimZip Integer form of the zip code
Zip Zip code that could be in various forms (nnnnn, nnnnn-nnnn, etc).
Country Country
AddrCatCode1 JDE related Category code, foreign key to the address category code 1
AddrCatCode2 JDE related Category code, foreign key to the address category code 2
Item Master Table (item_master_dim)
This table defines item masters (ie part numbers).
ItemMasterKey Primary key based on the sequence item_master_seq.
ShortItemId JDE related short item id.
SecondItemId JDE related 2nd item number
ThirdItemId JDE related 3rd item number
ItemCatCode1 JDE related category code, foreign key to the item category code1 table
ItemCatCode2 JDE related category code, foreign key to the item category code2 table
ItemDesc JDE related item master description
UOM JDE related primary unit of measure
Company Table (company_dim)
This table contains company records including the base currency.
CompanyKey Primary key based on the sequence company_seq.
CompanyId JDE related 5 character company id.
CompanyName JDE related company name
CurrencyCode JDE related currency code
CurrencyDesc JDE related currency description
Branch Plant Table (branch_plant_dim)
This table contains the Branch Plant information.
BranchPlantKey Primary Key based on sequence branch_plant_seq
BranchPlantId JDE related Branch Plant Id (12 character MCU).
CompanyKey Owning company for this branch, foreign key to Company table.
CarryingCost Carrying Cost percentage defined as a decimal
CostMethod JDE related Cost Method.
7/9/2018 Assignment 3 (Materialized Views) Page 12
BPName JDE related Branch Plant Name.
Inventory Transaction Fact Table (inventory_fact)
This table contains the inventory transactions facts. Integer keys are used to help limit the size of
the rows. The measures are unit cost, quantity, and extended cost.
InventoryKey Primary key based on the sequence inventory_seq.
BranchPlantKey Transaction Branch, Foreign key to the branch plant table.
DateKey Transaction Date, foreign key to the date table.
ItemMasterKey Transaction Part Number, foreign key to the item master table.
TransTypeKey Transaction Type, foreign key to the transaction type table.
CustVendorKey Optional address book key that is a foreign key to the customer vendor table.
This column allows null values. The column is not null only on sales and
purchasing transactions
UnitCost Unit cost with up to 4 decimals of precision.
Quantity Quantity with up to 4 decimals of precision.
ExtCost Extended Cost with up to 2 decimals of precision.
7/9/2018 Assignment 3 (Materialized Views) Page 13
Appendix B: CREATE TABLE Statements
-- drop exisiting tables...
drop table inventory_fact;
drop table date_dim;
drop table trans_type_dim;
drop table cust_vendor_dim;
drop table item_master_dim;
drop table branch_plant_dim;
drop table company_dim;
drop table addr_cat_code1;
drop table addr_cat_code2;
drop table item_cat_code1;
drop table item_cat_code2;
drop table zip_codes;
-- drop existing sequences...
drop sequence inventory_seq;
drop sequence date_seq;
drop sequence branch_plant_seq;
drop sequence cust_vendor_seq;
drop sequence item_master_seq;
drop sequence company_seq;
drop sequence addr_cat_code1_seq;
drop sequence addr_cat_code2_seq;
drop sequence item_cat_code1_seq;
drop sequence item_cat_code2_seq;
-- create sequences to be used as keys for specific dimension tables...
create sequence inventory_seq start with 1 increment by 1;
create sequence date_seq start with 1 increment by 1;
create sequence branch_plant_seq start with 1 increment by 1;
create sequence cust_vendor_seq start with 1 increment by 1;
create sequence item_master_seq start with 1 increment by 1;
create sequence company_seq start with 1 increment by 1;
create sequence addr_cat_code1_seq start with 1 increment by 1;
create sequence addr_cat_code2_seq start with 1 increment by 1;
create sequence item_cat_code1_seq start with 1 increment by 1;
create sequence item_cat_code2_seq start with 1 increment by 1;
-- create address category code...
create table addr_cat_code1(
AddrCatCodeKey integer not null,
AddrCatCodeId varchar2(4) not null,
AddrCatDesc varchar2(30) not null,
constraint addr_cat_code1_PK Primary Key(AddrCatCodeKey)
);
create table addr_cat_code2(
AddrCatCodeKey integer not null,
AddrCatCodeId varchar2(4) not null,
AddrCatDesc varchar2(30) not null,
constraint addr_cat_code2_PK Primary Key(AddrCatCodeKey)
);
7/9/2018 Assignment 3 (Materialized Views) Page 14
create table item_cat_code1(
ItemCatCodeKey integer not null,
ItemCatCodeId varchar2(4) not null,
ItemCatDesc varchar2(30) not null,
constraint item_cat_code1_PK Primary Key(ItemCatCodeKey)
);
create table item_cat_code2(
ItemCatCodeKey integer not null,
ItemCatCodeId varchar2(4) not null,
ItemCatDesc varchar2(30) not null,
constraint item_cat_code2_PK Primary Key(ItemCatCodeKey)
);
create table zip_codes(
ZipKey integer not null,
ZipCity varchar2(20) not null,
ZipState varchar2(2) not null,
ZipZip integer,
ZipConsec integer, -- number of consecutive zip codes...
ZipWeight integer, -- weight rating for zip code genreation
constraint zip_codes_PK Primary Key(ZipKey)
);
create table date_dim(
DateKey integer not Null,
DateJulian integer not Null, -- julian date in the format of yyyymmddd
CalDay integer not Null, -- from 1 to 31
CalMonth integer not Null, -- from 1 to 12
CalQuarter integer not Null, -- from 1 to 4
CalYear integer not Null, -- valid for 1900 to 2100
DayOfWeek integer not Null, -- 1 to 7 1 is Sunday, 2 is monday...
FiscalYear integer not Null,
FiscalPeriod integer not Null,
constraint date_dim_pk Primary Key(DateKey),
constraint date_dim_CalDay_CS check (CalDay >= 0 and CalDay <= 31),
constraint date_dim_CalMonth_CS check (CalMonth >= 0 and CalMonth <= 12),
constraint date_dim_CalQuarter_CS check (CalQuarter >= 0 and CalQuarter <=
4),
constraint date_dim_CalYear_CS check (CalYear >= 1900 and CalYear <=
2100),
constraint date_dim_DayOfWeek_CS check (DayOfWeek >= 0 and DayOfWeek <= 6)
);
create table trans_type_dim(
TransTypeKey integer not null,
TransTypeCodeId varchar2(2) not null,
TransDescription varchar2(30) not null,
constraint trans_type_pk Primary Key(TransTypeKey),
constraint trans_type_TransTypeId_CS check (TransTypeKey >= 1 and
TransTypeKey <= 9)
-- TransTypeId =1 then inventory adjustment (IA)
-- TransTypeId =2 then inventory transfer (IT)
-- TransTypeId =3 then inventory simple issue (IS)
-- TransTypeId =4 then purchase order receipt (OV)
-- TransTypeId =5 then sales order shipment (AR)
-- TransTypeId =6 then mfg issue (IM)
7/9/2018 Assignment 3 (Materialized Views) Page 15
-- TransTypeId =7 then mfg completion (IC)
-- TransTypeId =8 then mfg parent scrap (IS)
-- TransTypeId =9 then mfg component scrap (IZ)
);
create table cust_vendor_dim(
CustVendorKey integer not null,
AddrBookId integer not null unique,
Name varchar2(30) not null,
Address varchar2(30) not null,
City varchar2(20) not null,
State varchar2(2) not null,
PrimZip integer not null,
Zip varchar2(10) not null,
Country varchar2(3) default 'USA',
AddrCatCode1 integer,
AddrCatCode2 integer,
constraint cust_vend_dim_pk Primary Key(CustVendorKey),
constraint cust_vend_CatCode1_FK Foreign Key(AddrCatCode1) references
addr_cat_code1,
constraint cust_vend_CatCode2_FK Foreign Key(AddrCatCode2) references
addr_cat_code2
);
create table item_master_dim(
ItemMasterKey integer not null,
ShortItemId integer not null unique,
SecondItemId varchar2(30) not null,
ThirdItemId varchar2(30) not null,
ItemCatCode1 integer,
ItemCatCode2 integer,
ItemDesc varchar(30),
UOM varchar2(3),
constraint item_master_dim_pk Primary Key(ItemMasterkey),
constraint item_master_CatCode1_FK Foreign Key(ItemCatCode1) references
item_cat_code1,
constraint item_master_CatCode2_FK Foreign Key(ItemCatCode2) references
item_cat_code2
);
create table company_dim(
CompanyKey integer,
CompanyId varchar(5) not null,
CompanyName varchar2(30) not null,
CurrencyCode varchar2(5) not null,
CurrencyDesc varchar2(30)not null,
constraint company_dim_pk Primary Key (CompanyKey)
);
create table branch_plant_dim(
BranchPlantKey integer, -- sequence used for branch plant table
BranchPlantId varchar2(12) not null,
CompanyKey integer,
CarryingCost number(3,2) not null,
CostMethod varchar2(2) not null,
BPName varchar2(30),
constraint branch_plant_dim_pk Primary Key (BranchPlantKey),
7/9/2018 Assignment 3 (Materialized Views) Page 16
constraint branch_plant_CompanyId_FK Foreign Key(CompanyKey) references
company_dim
);
create table inventory_fact(
InventoryKey integer, -- sequence number used for fact table
BranchPlantKey integer not NULL,
DateKey integer not NULL,
ItemMasterKey integer not NULL,
TransTypeKey integer not NULL,
CustVendorKey integer,
UnitCost decimal(12,4),
Quantity decimal(9,4),
ExtCost decimal(14,2),
constraint inv_fact_PK PRIMARY Key(InventoryKey),
constraint inv_fact_Branch_Plant_FK Foreign Key(BranchPlantKey) references
branch_plant_dim,
constraint inv_fact_DateId_FK Foreign Key(DateKey) references Date_dim,
constraint inv_fact_CustVendorKey_FK Foreign Key(CustVendorKey) references
cust_vendor_dim,
constraint inv_fact_TransTypeId_FK Foreign Key(TransTypeKey) references
trans_type_dim,
constraint inv_fact_ShortItemId_FK Foreign Key(ItemMasterKey) references
item_master_dim
);