0% found this document useful (0 votes)
30 views

HW 6 SQL

The document contains SQL code that 1) maps production tables and columns to warehouse tables and columns, 2) loads data into a sales_for_RFM table, 3) creates a view to show total dollar amount by product, customer, and year, and 4) populates a product_sales table with a new payment_id column by joining several tables and grouping on relevant columns.

Uploaded by

api-570818594
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

HW 6 SQL

The document contains SQL code that 1) maps production tables and columns to warehouse tables and columns, 2) loads data into a sales_for_RFM table, 3) creates a view to show total dollar amount by product, customer, and year, and 4) populates a product_sales table with a new payment_id column by joining several tables and grouping on relevant columns.

Uploaded by

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

-- hw5.

sql
-- Justin Johnson
-- 1 Mapping of production to warehouse tables and columns
--
-- your answer here

-- PRODUCT.Description > PRODUCT.ProductType

-- PRODUCT.Description > PRODUCT.ProductName

-- (CUSTOMER.LastName +','+CUSTOMER.FirstName) >

-- CUSTOMER.CustomersName

-- CUSTOMER.EmailAddress > CUSTOMER.EmailDomain

-- CUSTOMER.Phone > CUSTOMER.PhoneAreaCode

-- (LINE_ITEM.Quantity * LINE_ITEM.UnitPrice) > PRODUCT-SALES.Total

-- 2 Load data for table SALES_FOR_RFM


insert into sales_for_rfm
(select t.timeid, i.CustomerID, i.InvoiceNumber, i.SubTotal
from hsd.invoice as i, hsddw.timeline as t
where i.invoicedate = t.date);

-- 3 create view of total dollar amount of each product for each year
create view total_dollar_amount as

SELECT c.CustomerId, c.CustomerName, c.City,


p.ProductNumber, p.ProductName,
t.Year,t.QuarterText,
SUM(ps.total) AS TotalDollarAmount
FROM customer c, product_sales ps, product p,
timeline t
WHERE c.CustomerId = ps.CustomerID
AND p.ProductNumber = ps.ProductNumber
AND t.TimeId = ps.TimeID
GROUP BY c.CustomerId, c.CustomerName, c.City,
p.ProductNumber, p.ProductName,
t.QuarterText, t.Year
ORDER BY c.CustomerName, t.Year, t.QuarterText;
-- 4 populate the product_sales table with the new payment_id column.
insert into hsddw.product_sales

(timeid, customerid, productnumber, quantity, unitprice, total, payment_id)

select c.timeid, a.customerid, b.productnumber, sum(b.quantity), min(b.unitprice), sum(b.total),


p.payment_type_id

from hsd.invoice a, hsd.line_item b, hsddw.timeline c, hsddw.payment_type p

where a.invoicenumber = b.invoicenumber and a.invoicedate =c.date and a.paymenttype =


p.payment_type

group by c.timeid, a.customerid, b.productnumber, p.payment_type_id;

You might also like