Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 3.60 KB | None | 0 0
  1. Week 9 Practical
  2. This week’s practical involves a little research ON how TO perform a couple OF complex tasks:
  3. 1.  Creating a Stored PROCEDURE that returns records IN a specific ORDER
  4. 2.  A rather complex UPDATE query involving a TABLE join.  This TYPE OF query IS often used TO fix errors WHERE a mistake has been made that affects data across a whole TABLE (the amount the customer paid IN this CASE).  
  5. Question 1: Find the NUMBER OF customers WITH the FIRST name Lisa, Anne OR Mary
  6. Answer
  7. CREATE PROCEDURE NumberOfCustomersWithName
  8.  
  9. AS
  10.  
  11. SELECT c.firstName, COUNT(c.customerID) AS customerCount
  12.  
  13. FROM Customers AS c
  14.  
  15. WHERE c.firstName = 'Lisa' OR c.firstName = 'Anne' OR c.firstName = 'Mary'
  16.  
  17. GROUP BY c.firstName
  18.  
  19. EXECUTE NumberOfCustomersWithName
  20.  
  21.  
  22.  
  23.  
  24. Question 2: CREATE a Stored PROCEDURE that accepts a search parameter AND finds customer FIRST names OR LAST names that START WITH the entered VALUE.  IF no search text IS provided, ALL customer names are returned.  ORDER the results BY those WITH a matching firstName ascending FIRST (AND WHEN no search text IS provided), THEN LAST name ascending SECOND AND BY DEFAULT.
  25. AS an example, IF you pass ‘Lis’ you should get records matching ‘Lisa’ FIRST AND ‘Lisandra’ SECOND!
  26. Answer
  27. CREATE PROCEDURE searchCustomers
  28. @fName VARCHAR(100),
  29. @lName VARCHAR(100)
  30.  
  31. AS
  32.  
  33. IF (@fName IS NULL AND @lName IS NULL)
  34. BEGIN
  35.     SELECT c.firstName, c.lastName
  36.  
  37.     FROM Customers AS c
  38.  
  39.     ORDER BY c.firstName ASC, c.lastName ASC
  40. END
  41.  
  42. ELSE
  43. BEGIN
  44.     SELECT c.firstName, c.lastName
  45.  
  46.     FROM Customers AS c
  47.  
  48.     WHERE c.firstName LIKE @fName + '%' OR c.lastName LIKE @lName + '%'
  49.  
  50.     ORDER BY c.firstName ASC, c.lastName ASC
  51. END
  52.  
  53. EXECUTE searchCustomers NULL,NULL
  54.  
  55.  
  56.  
  57. Question 3: UPDATE query WITH a TABLE join!
  58. The profit margins ON the sales IN our store are way too small.  Your task IS TO “cook the books” AND inflate the amounts paid TO make it more realistic!  You will need TO research how TOUPDATE a TABLE using a join” (you will find loads OF example via Google).  TO further complicate the issue, the UPDATE will be different depending ON the price OF the item (ie, you will need a CASE statement AS well!). The other thing you need TO take INTO account IS the qty OF an item purchased during the transaction.
  59. USE the following mark-up guidelines FOR your UPDATE PROCEDURE:
  60. 1.  Price <= $15 THEN the sale price will be 5 times the CURRENT price
  61. 2.  Price > $15 AND <= $25, the sale price will be 3.5 times the CURRENT price
  62. 3.  Price > $25 AND <= $50, the sale price will be 2.5 times the CURRENT price
  63. 4.  Price > 50, the sale price will be 2 times the CURRENT price
  64. It IS suggested you get the price calculation working using a SELECT - CASE query FIRST, THEN try TO incorporate the result INTO an UPDATE query.  Have your SELECT query checked before moving TO including it INTO the UPDATE query
  65. Price SELECT Query
  66. ANSWER
  67. SELECT si.price,
  68.         (
  69.             CASE
  70.             WHEN si.price <= 15
  71.             THEN si.price*5
  72.  
  73.             WHEN si.price > 15 AND si.price <= 25
  74.             THEN si.price*3.5
  75.  
  76.             WHEN si.price > 25 AND si.price <= 50
  77.             THEN si.price*2.5
  78.  
  79.             ELSE
  80.             si.price*2
  81.             END
  82.         )
  83.         AS calc
  84.         FROM ShopItems AS si
  85. Price UPDATE Query
  86. ANSWER
  87. UPDATE CustomerPurchases
  88.     SET CustomerPurchases.totalDue =
  89.     (
  90.         SELECT si.price,
  91.         (
  92.             CASE
  93.             WHEN si.price <= 15
  94.             THEN si.price*5
  95.  
  96.             WHEN si.price > 15 AND si.price <= 25
  97.             THEN si.price*3.5
  98.  
  99.             WHEN si.price > 25 AND si.price <= 50
  100.             THEN si.price*2.5
  101.  
  102.             ELSE
  103.             si.price*2
  104.             END
  105.         )
  106.         AS calc
  107.         FROM ShopItems AS si
  108.     )
  109.     FROM CustomerPurchases JOIN ShopItems
  110.     ON CustomerPurchases.itemID = ShopItems.itemID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement