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