Skip to content

Commit

Permalink
Added challenge #4
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinFrizzell committed Oct 8, 2023
1 parent a6d04cc commit f69bf89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Challenges/#4 - ufn_GetTotalOrderWeight.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Requirement: ufn_GetTotalOrderWeight

-- Purpose:
-- We need a scalar function that, given an OrderID, will return the total weight of all the items in that order.

-- Specifics:
-- The function should be named ufn_GetTotalOrderWeight.
-- The input to the function should be an OrderID.
-- The function should return the total weight as a decimal.
-- The weight of each item can be calculated from the StockItemTransactions table. Each stock item transaction related to an order has a Quantity and is linked to a StockItem which has a TypicalWeightPerUnit.
-- The total weight for the order can be determined by summing up the products of Quantity and TypicalWeightPerUnit for all stock items in that order.

-- Notes:
-- Ensure to handle cases where the OrderID might not be present in the database.
-- Return a value of 0 if there are no items or if the order doesn't exist.

-- Solution: WideWorldImporters\Challenges\Functions\ufn_GetTotalOrderWeight.sql

SELECT TOP 100 OrderID, OrderDate, Challenges.ufn_GetTotalOrderWeight(OrderID) AS TotalOrderWeight FROM Sales.Orders
ORDER BY OrderDate DESC
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE FUNCTION Challenges.ufn_GetTotalOrderWeight ( @OrderID INT )
RETURNS DECIMAL (18, 3)
AS
BEGIN
DECLARE @Weight DECIMAL (18, 3)
SELECT @Weight = COALESCE(SUM(ol.PickedQuantity * si.TypicalWeightPerUnit),0) FROM Sales.OrderLines AS ol
INNER JOIN Warehouse.StockItems AS si
ON ol.StockItemID = si.StockItemID
WHERE ol.OrderID = @OrderID
RETURN(@Weight)
END

GO

0 comments on commit f69bf89

Please sign in to comment.