Skip to content

Commit

Permalink
Sample files
Browse files Browse the repository at this point in the history
  • Loading branch information
TexanInParis committed Aug 29, 2016
1 parent 4471cf1 commit 326fee5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
21 changes: 21 additions & 0 deletions Microsoft SQL Server/Chapter 04/Listing 4.017.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ GO

DROP VIEW CustomerProducts;
GO

-- Sample query that searches products correctly:
CREATE VIEW CustomerProducts AS
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Order_Details
ON Orders.OrderNumber = Order_Details.OrderNumber
INNER JOIN Products
ON Products.ProductNumber = Order_Details.ProductNumber;
GO

DROP VIEW CustomerProducts;
GO
12 changes: 9 additions & 3 deletions Microsoft SQL Server/Chapter 04/Listing 4.018.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ GO

-- Sample query that searches products correctly:
CREATE VIEW ProdsOfInterest AS
SELECT Products.ProductName
SELECT Products.ProductName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Products
WHERE ProductName LIKE '%Skateboard%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Gloves%';
GO

Expand Down
26 changes: 19 additions & 7 deletions Microsoft SQL Server/Chapter 04/Listing 4.019.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ GO

-- Sample query that searches products correctly:
CREATE VIEW CustomerProducts AS
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName, Products.ProductName
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Order_Details
Expand All @@ -52,22 +58,28 @@ INNER JOIN Products
GO

CREATE VIEW ProdsOfInterest AS
SELECT Products.ProductName
SELECT Products.ProductName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Products
WHERE ProductName LIKE '%Skateboard%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Gloves%';
GO

SELECT DISTINCT CustomerID, CustFirstName, CustLastName
FROM CustomerProducts AS CP1
WHERE NOT EXISTS
(SELECT ProductName FROM ProdsOfInterest AS PofI
(SELECT ProductCategory FROM ProdsOfInterest AS PofI
WHERE NOT EXISTS
(SELECT CustomerID FROM CustomerProducts AS CP2
WHERE CP2.CustomerID = CP1.CustomerID
AND CP2.ProductName = PofI.ProductName));
AND CP2.ProductCategory = PofI.ProductCategory));
GO

DROP VIEW CustomerProducts;
Expand Down
30 changes: 21 additions & 9 deletions Microsoft SQL Server/Chapter 04/Listing 4.020.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ GO

-- Sample query that searches products correctly:
CREATE VIEW CustomerProducts AS
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName, Products.ProductName
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Order_Details
Expand All @@ -50,20 +56,26 @@ INNER JOIN Products
GO

CREATE VIEW ProdsOfInterest AS
SELECT Products.ProductName
SELECT Products.ProductName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Products
WHERE ProductName LIKE '%Skateboard%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Gloves%';
GO

SELECT CP.CustomerID, CP.CustFirstName, CP.CustLastName
FROM CustomerProducts AS CP CROSS JOIN ProdsOfInterest AS PofI
WHERE CP.ProductName = PofI.ProductName
FROM CustomerProducts CP, ProdsOfInterest PofI
WHERE CP.ProductCategory = PofI.ProductCategory
GROUP BY CP.CustomerID, CP.CustFIrstName, CP.CustLastName
HAVING COUNT(CP.ProductName) =
(SELECT COUNT(ProductName) FROM ProdsOfInterest);
HAVING COUNT(CP.ProductCategory) =
(SELECT COUNT(ProductCategory) FROM ProdsOfInterest);
GO

DROP VIEW CustomerProducts;
Expand Down

0 comments on commit 326fee5

Please sign in to comment.