-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6d04cc
commit f69bf89
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
WideWorldImporters/Challenges/Functions/ufn_GetTotalOrderWeight.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |