I want to create a query that shows the StoreName, StoreCountry, ItemNumber, ItemName, and that items percentage of the total sales for an item. This is my code below; it is not working.
I want:
The total quantity of a given as a percentage of the total quantity sold in the entire result set
/* Displays Store Name and Items that were Purchased from that Store that have been in Sales Transactions and the Items Sales percentage */I want:
The total quantity of a given as a percentage of the total quantity sold in the entire result set
CREATE VIEW STORE_SALES_ITEMS99 ([Store Name], [Store Country], [Item #], [Item Name], [% of Sales])AS
SELECT DISTINCT st.StoreName, st.StoreCountry, si.ItemNumberSK, si.ItemName,(si.Qty)/(SUM(si.Qty))
FROM STORE st, ITEM_PURCHASE ip, SALES_ITEM si
WHERE st.StoreNumberSK = ip.StoreNumberSK
AND ip.ItemNumberSK = si.ItemNumberSK
GROUP BY st.StoreName, st.StoreCountry, si.ItemNumberSK, si.ItemName
CREATE TABLE STORE(
StoreNumberSK int NOT NULL IDENTITY (1, 1),
StoreName char(40) NOT NULL,
StoreAddress char(30) NOT NULL,
StoreCity char(50) NOT NULL,
StoreCountry char(50) NOT NULL,
StorePhone numeric(10, 0) NOT NULL,
StoreFax numeric(10, 0) NOT NULL,
StoreEmail varchar(50) NOT NULL,
StoreContact char(30) NOT NULL,
CONSTRAINT StorePK PRIMARY KEY (StoreNumberSK),
/* Creates ITEM_PURCHASE table */
CREATE TABLE ITEM_PURCHASE(
ItemNumberSK int NOT NULL IDENTITY (10000, 1),
StoreNumberSK int NOT NULL,
ItemName char(50) NOT NULL,
Date smalldatetime NOT NULL,
LocalCurrencyAmt decimal (12, 6) NOT NULL,
ExchangeRate decimal (12, 6) NOT NULL,
Quantity numeric(7, 0) NOT NULL,
CONSTRAINT Item_PurchasePK PRIMARY KEY (ItemNumberSK),
CONSTRAINT Item_PurchaseFK FOREIGN KEY (StoreNumberSK) REFERENCES STORE (StoreNumberSK)
);
* Creates SALES_ITEM table */
CREATE TABLE SALES_ITEM(
InvoiceNumberSK int NOT NULL,
ItemNumberSK int NOT NULL,
ItemName char(50) NULL,
Qty numeric(7, 0) NOT NULL,
UnitPrice money NULL,
ExtendedPrice AS CASE
WHEN UnitPrice > 0 AND Qty > 0 Then UnitPrice * Qty
END
CONSTRAINT Sales_ItemPK PRIMARY KEY (InvoiceNumberSK, ItemNumberSK),
CONSTRAINT Sales_ItemInvoiceNumberFK FOREIGN KEY (InvoiceNumberSK) REFERENCES SALES (InvoiceNumberSK),
CONSTRAINT Sales_ItemItemNumberSKFK FOREIGN KEY (ItemNumberSK) REFERENCES WAREHOUSE (ItemNumberSK)
);