create or replace view vwProducts_Above_Average_Price
as
select distinct ProductName, UnitPrice
from Products
where UnitPrice > (select avg(UnitPrice) from Products)
order by UnitPrice;
create or replace view vwQuarterly_Orders_by_Product
as
select a.ProductName,
d.CompanyName,
year(OrderDate) as OrderYear,
format(sum(case quarter(c.OrderDate) when '1'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 1",
format(sum(case quarter(c.OrderDate) when '2'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 2",
format(sum(case quarter(c.OrderDate) when '3'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 3",
format(sum(case quarter(c.OrderDate) when '4'
then b.UnitPrice*b.Quantity*(1-b.Discount) else 0 end), 0) "Qtr 4"
from Products a
inner join Order_Details b on a.ProductID = b.ProductID
inner join Orders c on c.OrderID = b.OrderID
inner join Customers d on d.CustomerID = c.CustomerID
where c.OrderDate between date('1997-01-01') and date('1997-12-31')
group by a.ProductName,
d.CompanyName,
year(OrderDate)
order by a.ProductName, d.CompanyName;
create or replace view vwInvoice
as
select distinct b.ShipName,
b.ShipAddress,
b.ShipCity,
b.ShipRegion,
b.ShipPostalCode,
b.ShipCountry,
b.CustomerID,
c.CompanyName,
c.Address,
c.City,
c.Region,
c.PostalCode,
c.Country,
concat(d.FirstName, ' ', d.LastName) as Salesperson,
b.OrderID,
b.OrderDate,
b.RequiredDate,
b.ShippedDate,
a.CompanyName as ShipperName,
e.ProductID,
f.ProductName,
e.UnitPrice,
e.Quantity,
e.Discount,
e.UnitPrice * e.Quantity * (1 - e.Discount) as ExtendedPrice,
b.Freight
from Shippers a
inner join Orders b on a.ShipperID = b.ShipVia
inner join Customers c on c.CustomerID = b.CustomerID
inner join Employees d on d.EmployeeID = b.EmployeeID
inner join Order_Details e on b.OrderID = e.OrderID
inner join Products f on f.ProductID = e.ProductID
order by b.ShipName;
create or replace view vwUnits_In_Stock
as
select c.CategoryName as "Product Category",
case when s.Country in
('UK','Spain','Sweden','Germany','Norway',
'Denmark','Netherlands','Finland','Italy','France')
then 'Europe'
when s.Country in ('USA','Canada','Brazil')
then 'America'
else 'Asia-Pacific'
end as "Supplier Continent",
sum(p.UnitsInStock) as UnitsInStock
from Suppliers s
inner join Products p on p.SupplierID=s.SupplierID
inner join Categories c on c.CategoryID=p.CategoryID
group by c.CategoryName,
case when s.Country in
('UK','Spain','Sweden','Germany','Norway',
'Denmark','Netherlands','Finland','Italy','France')
then 'Europe'
when s.Country in ('USA','Canada','Brazil')
then 'America'
else 'Asia-Pacific'
end;
create or replace view vwSales_By_Category
as
select distinct a.CategoryID,
a.CategoryName,
b.ProductName,
sum(round(y.UnitPrice * y.Quantity * (1 - y.Discount), 2)) as ProductSales
from Order_Details y
inner join Orders d on d.OrderID = y.OrderID
inner join Products b on b.ProductID = y.ProductID
inner join Categories a on a.CategoryID = b.CategoryID
where d.OrderDate between date('1997/1/1') and date('1997/12/31')
group by a.CategoryID, a.CategoryName, b.ProductName
order by a.CategoryName, b.ProductName, ProductSales;
create or replace view vw10Most_Expensive_Products
as
select distinct ProductName as Ten_Most_Expensive_Products,
UnitPrice
from Products
order by UnitPrice desc
limit 10;
create or replace view vwCustomer_Supplier_by_City
as
select City, CompanyName, ContactName, 'Customers' as Relationship
from Customers
union
select City, CompanyName, ContactName, 'Suppliers'
from Suppliers
order by City, CompanyName;