279 lines
10 KiB
Plaintext
279 lines
10 KiB
Plaintext
/*
|
|
Drop Views & sp
|
|
*/
|
|
if exists(select 1 from sys.views where name=@name and type='v')
|
|
drop view [Orders Qry];
|
|
go
|
|
|
|
|
|
|
|
/****** Object: View Orders Qry Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Orders_Qry
|
|
AS
|
|
SELECT Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate,
|
|
Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity,
|
|
Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry,
|
|
Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country
|
|
FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
|
|
|
|
|
|
/****** Object: View Quarterly Orders Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Quarterly_Orders
|
|
AS
|
|
SELECT DISTINCT Customers.CustomerID, Customers.CompanyName, Customers.City, Customers.Country
|
|
FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
|
|
WHERE Orders.OrderDate BETWEEN '19970101' And '19971231'
|
|
|
|
|
|
/****** Object: View Invoices Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Invoices
|
|
AS
|
|
SELECT Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode,
|
|
Orders.ShipCountry, Orders.CustomerID, Customers.CompanyName AS CustomerName, Customers.Address, Customers.City,
|
|
Customers.Region, Customers.PostalCode, Customers.Country,
|
|
(FirstName + ' ' + LastName) AS Salesperson,
|
|
Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Shippers.CompanyName As ShipperName,
|
|
[Order Details].ProductID, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity,
|
|
[Order Details].Discount,
|
|
(CONVERT(money,([Order Details].UnitPrice*Quantity*(1-Discount)/100))*100) AS ExtendedPrice, Orders.Freight
|
|
FROM Shippers INNER JOIN
|
|
(Products INNER JOIN
|
|
(
|
|
(Employees INNER JOIN
|
|
(Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID)
|
|
ON Employees.EmployeeID = Orders.EmployeeID)
|
|
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID)
|
|
ON Products.ProductID = [Order Details].ProductID)
|
|
ON Shippers.ShipperID = Orders.ShipVia
|
|
|
|
|
|
/****** Object: View Product Sales for 1997 Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Product_Sales_for_1997
|
|
AS
|
|
SELECT Categories.CategoryName, Products.ProductName,
|
|
Sum(CONVERT(money,([Order Details].UnitPrice*Quantity*(1-Discount)/100))*100) AS ProductSales
|
|
FROM (Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID)
|
|
INNER JOIN (Orders
|
|
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID)
|
|
ON Products.ProductID = [Order Details].ProductID
|
|
WHERE (((Orders.ShippedDate) Between '19970101' And '19971231'))
|
|
GROUP BY Categories.CategoryName, Products.ProductName
|
|
|
|
/****** Object: View Current Product List Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Current_Product_List
|
|
AS
|
|
SELECT Product_List.ProductID, Product_List.ProductName
|
|
FROM Products AS Product_List
|
|
WHERE (((Product_List.Discontinued)=0))
|
|
|
|
|
|
/****** Object: View Order_Details Extended Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Order_Details_Extended
|
|
AS
|
|
SELECT OD.OrderID, OD.ProductID, Products.ProductName,
|
|
OD.UnitPrice, OD.Quantity, OD.Discount,
|
|
(CONVERT(money,(OD.UnitPrice*Quantity*(1-Discount)/100))*100) AS ExtendedPrice
|
|
FROM Products INNER JOIN [Order Details] OD ON Products.ProductID = OD.ProductID
|
|
|
|
|
|
/****** Object: View Products Above Average Price Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Products_Above_Average_Price AS
|
|
SELECT Products.ProductName, Products.UnitPrice
|
|
FROM Products
|
|
WHERE Products.UnitPrice>(SELECT AVG(UnitPrice) From Products)
|
|
--ORDER BY Products.UnitPrice DESC
|
|
|
|
|
|
/****** Object: View Products by Category Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Products_by_Category AS
|
|
SELECT Categories.CategoryName, Products.ProductName, Products.QuantityPerUnit, Products.UnitsInStock, Products.Discontinued
|
|
FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID
|
|
WHERE Products.Discontinued <> 1
|
|
--ORDER BY Categories.CategoryName, Products.ProductName
|
|
|
|
|
|
/****** Object: View Alphabetical list of products Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Alphabetical_list_of_products AS
|
|
SELECT Products.*, Categories.CategoryName
|
|
FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID
|
|
WHERE (((Products.Discontinued)=0))
|
|
|
|
|
|
/****** Object: View Order Subtotals Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Order_Subtotals AS
|
|
SELECT OD.OrderID, Sum(CONVERT(money,(OD.UnitPrice*Quantity*(1-Discount)/100))*100) AS Subtotal
|
|
FROM [Order Details] OD
|
|
GROUP BY OD.OrderID
|
|
|
|
|
|
/****** Object: View Customer and Suppliers by City Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Customer_and_Suppliers_by_City
|
|
AS
|
|
SELECT City, CompanyName, ContactName, 'Customers' AS Relationship
|
|
FROM Customers
|
|
UNION SELECT City, CompanyName, ContactName, 'Suppliers'
|
|
FROM Suppliers
|
|
--ORDER BY City, CompanyName
|
|
|
|
|
|
/****** Object: View Sales Totals by Amount Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Sales_Totals_by_Amount
|
|
AS
|
|
SELECT
|
|
Order_Subtotals.Subtotal AS SaleAmount, Orders.OrderID, Customers.CompanyName, Orders.ShippedDate
|
|
FROM Customers INNER JOIN
|
|
(Orders INNER JOIN Order_Subtotals ON Orders.OrderID = Order_Subtotals.OrderID)
|
|
ON Customers.CustomerID = Orders.CustomerID
|
|
WHERE (Order_Subtotals.Subtotal >2500) AND (Orders.ShippedDate BETWEEN '19970101' And '19971231')
|
|
|
|
|
|
/****** Object: View Summary of Sales by Quarter Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Summary_of_Sales_by_Quarter
|
|
AS
|
|
SELECT Orders.ShippedDate, Orders.OrderID, Order_Subtotals.Subtotal
|
|
FROM Orders INNER JOIN Order_Subtotals ON Orders.OrderID = Order_Subtotals.OrderID
|
|
WHERE Orders.ShippedDate IS NOT NULL
|
|
--ORDER BY Orders.ShippedDate
|
|
|
|
|
|
/****** Object: View Summary of Sales by Year Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Summary_of_Sales_by_Year
|
|
AS
|
|
SELECT Orders.ShippedDate, Orders.OrderID, Order_Subtotals.Subtotal
|
|
FROM Orders INNER JOIN Order_Subtotals ON Orders.OrderID = Order_Subtotals.OrderID
|
|
WHERE Orders.ShippedDate IS NOT NULL
|
|
--ORDER BY Orders.ShippedDate
|
|
|
|
|
|
/****** Object: View Sales by Category Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Sales_by_Category
|
|
AS
|
|
SELECT Categories.CategoryID, Categories.CategoryName, Products.ProductName,
|
|
Sum([Order Details Extended].ExtendedPrice) AS ProductSales
|
|
FROM Categories INNER JOIN
|
|
(Products INNER JOIN
|
|
(Orders INNER JOIN [Order Details Extended] ON Orders.OrderID = [Order Details Extended].OrderID)
|
|
ON Products.ProductID = [Order Details Extended].ProductID)
|
|
ON Categories.CategoryID = Products.CategoryID
|
|
WHERE Orders.OrderDate BETWEEN '19970101' And '19971231'
|
|
GROUP BY Categories.CategoryID, Categories.CategoryName, Products.ProductName
|
|
--ORDER BY Products.ProductName
|
|
|
|
|
|
/****** Object: View Category Sales for 1997 Script Date: 08/12/2011 11:46:04 ******/
|
|
create view Category_Sales_for_1997
|
|
AS
|
|
SELECT v1997.CategoryName, Sum(v1997.ProductSales) AS CategorySales
|
|
FROM Product_Sales_for_1997 v1997
|
|
GROUP BY v1997.CategoryName
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****** Object: StoredProcedure SalesByCategory Script Date: 08/12/2011 11:46:02 ******/
|
|
CREATE PROCEDURE SalesByCategory
|
|
(
|
|
@CategoryName varchar(15),
|
|
@OrdYear varchar(4) = '1998'
|
|
)
|
|
AS
|
|
IF @OrdYear != '1996' AND @OrdYear != '1997' AND @OrdYear != '1998'
|
|
BEGIN
|
|
SELECT @OrdYear = '1998'
|
|
END
|
|
|
|
SELECT ProductName,
|
|
TotalPurchase=ROUND(SUM(CONVERT(decimal(14,2), OD.Quantity * (1-OD.Discount) * OD.UnitPrice)), 0)
|
|
FROM Order_Details OD, Orders O, Products P, Categories C
|
|
WHERE OD.OrderID = O.OrderID
|
|
AND OD.ProductID = P.ProductID
|
|
AND P.CategoryID = C.CategoryID
|
|
AND C.CategoryName = @CategoryName
|
|
AND SUBSTRING(CONVERT(varchar(22), O.OrderDate, 111), 1, 4) = @OrdYear
|
|
GROUP BY ProductName
|
|
ORDER BY ProductName
|
|
|
|
|
|
/****** Object: StoredProcedure CustOrdersOrders Script Date: 08/12/2011 11:46:02 ******/
|
|
CREATE PROCEDURE CustOrdersOrders
|
|
(
|
|
@CustomerID char(5)
|
|
)
|
|
AS
|
|
SELECT OrderID,
|
|
OrderDate,
|
|
RequiredDate,
|
|
ShippedDate
|
|
FROM Orders
|
|
WHERE CustomerID = @CustomerID
|
|
ORDER BY OrderID
|
|
|
|
|
|
/****** Object: StoredProcedure CustOrderHist Script Date: 08/12/2011 11:46:01 ******/
|
|
CREATE PROCEDURE CustOrderHist
|
|
(
|
|
@CustomerID char(5)
|
|
)
|
|
AS
|
|
SELECT ProductName, Total=SUM(Quantity)
|
|
FROM Products P, Order_Details OD, Orders O, Customers C
|
|
WHERE C.CustomerID = @CustomerID
|
|
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID
|
|
GROUP BY ProductName
|
|
|
|
|
|
/****** Object: StoredProcedure CustOrdersDetail Script Date: 08/12/2011 11:46:01 ******/
|
|
CREATE PROCEDURE CustOrdersDetail
|
|
(
|
|
@OrderID int
|
|
)
|
|
AS
|
|
SELECT ProductName,
|
|
UnitPrice=ROUND(Od.UnitPrice, 2),
|
|
Quantity,
|
|
Discount=CONVERT(int, Discount * 100),
|
|
ExtendedPrice=ROUND(CONVERT(money, Quantity * (1 - Discount) * Od.UnitPrice), 2)
|
|
FROM Products P, Order_Details Od
|
|
WHERE Od.ProductID = P.ProductID and Od.OrderID = @OrderID
|
|
|
|
|
|
/****** Object: StoredProcedure Ten Most Expensive Products Script Date: 08/12/2011 11:46:02 ******/
|
|
create procedure Ten_Most_Expensive_Products
|
|
AS
|
|
SET ROWCOUNT 10
|
|
SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
|
|
FROM Products
|
|
ORDER BY Products.UnitPrice DESC
|
|
|
|
|
|
/****** Object: StoredProcedure Sales by Year Script Date: 08/12/2011 11:46:02 ******/
|
|
create procedure Sales_by_Year
|
|
(
|
|
@Beginning_Date Date,
|
|
@Ending_Date Date
|
|
)
|
|
AS
|
|
SELECT Orders.ShippedDate, Orders.OrderID, Order_Subtotals.Subtotal, DATENAME(yy,ShippedDate) AS Year
|
|
FROM Orders INNER JOIN Order_Subtotals ON Orders.OrderID = Order_Subtotals.OrderID
|
|
WHERE Orders.ShippedDate Between @Beginning_Date And @Ending_Date
|
|
|
|
|
|
/****** Object: StoredProcedure Employee Sales by Country Script Date: 08/12/2011 11:46:02 ******/
|
|
create procedure Employee_Sales_by_Country
|
|
(
|
|
@Beginning_Date DateTime,
|
|
@Ending_Date DateTime
|
|
)
|
|
AS
|
|
SELECT Employees.Country, Employees.LastName, Employees.FirstName, Orders.ShippedDate, Orders.OrderID, Order_Subtotals.Subtotal AS SaleAmount
|
|
FROM Employees INNER JOIN
|
|
(Orders INNER JOIN Order_Subtotals ON Orders.OrderID = Order_Subtotals.OrderID)
|
|
ON Employees.EmployeeID = Orders.EmployeeID
|
|
WHERE Orders.ShippedDate Between @Beginning_Date And @Ending_Date
|
|
|