Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/databases/futon-manufacturing/02-sample-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ GO
-- Bill of Materials - Multi-Level
-- =============================================

DECLARE @EachUnit INT = (SELECT UnitID FROM UnitOfMeasure WHERE UnitCode = 'EA');
DECLARE @YardUnit INT = (SELECT UnitID FROM UnitOfMeasure WHERE UnitCode = 'YD');
DECLARE @PoundUnit INT = (SELECT UnitID FROM UnitOfMeasure WHERE UnitCode = 'LB');

-- Level 1: Pillows (Components made from raw materials)
-- Standard Polyester Pillow
INSERT INTO BillOfMaterials (ParentItemID, ComponentItemID, Quantity, UnitID, BOMLevel, ScrapRate) VALUES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ WITH BOMRecursive AS (
c.ItemName AS ComponentItemName,
c.ItemTypeID,
t.TypeName AS ComponentType,
b.Quantity,
CAST(b.Quantity AS DECIMAL(18,4)) AS Quantity,
b.UnitID,
u.UnitCode,
b.ScrapRate,
Expand Down Expand Up @@ -52,7 +52,7 @@ WITH BOMRecursive AS (
c.ItemName,
c.ItemTypeID,
t.TypeName,
br.EffectiveQuantity * b.Quantity AS Quantity,
CAST(br.EffectiveQuantity * b.Quantity AS DECIMAL(18,4)) AS Quantity,
b.UnitID,
u.UnitCode,
b.ScrapRate,
Expand Down Expand Up @@ -102,7 +102,7 @@ WITH WhereUsedRecursive AS (
p.ItemCode AS ParentItemCode,
p.ItemName AS ParentItemName,
t.TypeName AS ParentType,
b.Quantity,
CAST(b.Quantity AS DECIMAL(18,4)) AS Quantity,
u.UnitCode,
1 AS Level,
CAST(c.ItemCode + ' used in ' + p.ItemCode AS NVARCHAR(MAX)) AS UsagePath
Expand All @@ -124,7 +124,7 @@ WITH WhereUsedRecursive AS (
p.ItemCode,
p.ItemName,
t.TypeName,
wu.Quantity * b.Quantity AS Quantity,
CAST(wu.Quantity * b.Quantity AS DECIMAL(18,4)) AS Quantity,
u.UnitCode,
wu.Level + 1,
CAST(wu.UsagePath + ' > ' + p.ItemCode AS NVARCHAR(MAX))
Expand Down Expand Up @@ -578,7 +578,7 @@ SELECT
t.TypeName AS ItemType,
inv.QuantityOnHand,
inv.QuantityAvailable,
ts.QuantityIssued AS Annual Usage,
ts.QuantityIssued AS [Annual Usage],
ts.QuantityReceived AS AnnualReceipts,
ts.TransactionCount,
CAST(ts.QuantityIssued / NULLIF(inv.QuantityOnHand, 0) AS DECIMAL(10,2)) AS TurnoverRatio,
Expand Down
38 changes: 28 additions & 10 deletions samples/databases/futon-manufacturing/04-sample-queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -373,18 +373,36 @@ GO
PRINT '';
PRINT '20. ABC Inventory Classification (by value)';
PRINT '---------------------------------------------------------------------';
WITH InventoryValue AS (
WITH InventoryTotals AS
(
SELECT
ItemCode,
ItemName,
ItemType,
QuantityOnHand,
InventoryValue,
SUM(InventoryValue) OVER () AS TotalInventoryValue,
InventoryValue * 100.0 / SUM(InventoryValue) OVER () AS PercentOfTotal,
SUM(InventoryValue * 100.0 / SUM(InventoryValue) OVER ())
OVER (ORDER BY InventoryValue DESC) AS CumulativePercent
SUM(InventoryValue) OVER () AS TotalInventoryValue
FROM vw_InventoryValuation
),
InventoryValueAnalysis AS
(
SELECT
ItemCode,
ItemName,
ItemType,
QuantityOnHand,
InventoryValue,

InventoryValue * 100.0
/ NULLIF(TotalInventoryValue, 0) AS PercentOfTotal,

SUM(InventoryValue) OVER
(
ORDER BY InventoryValue DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) * 100.0
/ NULLIF(TotalInventoryValue, 0) AS CumulativePercent
FROM InventoryTotals
)
SELECT
ItemCode,
Expand All @@ -399,7 +417,7 @@ SELECT
WHEN CumulativePercent <= 95 THEN 'B'
ELSE 'C'
END AS ABCClass
FROM InventoryValue
FROM InventoryValueAnalysis
WHERE InventoryValue > 0
ORDER BY InventoryValue DESC;
GO
Expand Down Expand Up @@ -444,10 +462,10 @@ SELECT
END AS Status
FROM MaterialNeeds mn
LEFT JOIN (
SELECT ItemID, i.ItemCode, SUM(QuantityAvailable) AS QuantityAvailable
SELECT i.ItemID, i.ItemCode, SUM(QuantityAvailable) AS QuantityAvailable
FROM Inventory inv
INNER JOIN Items i ON inv.ItemID = i.ItemID
GROUP BY ItemID, i.ItemCode
GROUP BY i.ItemID, i.ItemCode
) inv ON mn.ComponentItemCode = inv.ItemCode
ORDER BY mn.ComponentType, mn.ComponentItemName;
GO
Expand All @@ -462,7 +480,7 @@ PRINT '---------------------------------------------------------------------';
SELECT
'Total Inventory Value' AS KPI,
CAST(SUM(InventoryValue) AS DECIMAL(18,2)) AS Value,
NULL AS Percent,
NULL AS [Percent],
'USD' AS Unit
FROM vw_InventoryValuation

Expand All @@ -471,7 +489,7 @@ UNION ALL
SELECT
'Production Orders On Time',
COUNT(*),
CAST(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM ProductionOrder WHERE Status = 'Completed') AS DECIMAL(5,2)),
CAST(COUNT(*) * 100.0 / NULLIF((SELECT COUNT(*) FROM ProductionOrder WHERE Status = 'Completed'), 0) AS DECIMAL(5,2)),
'%'
FROM ProductionOrder
WHERE Status = 'Completed' AND ActualCompletionDate <= PlannedCompletionDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ ALTER TABLE SalesOrder ADD SalesChannelID INT NULL;
ALTER TABLE SalesOrder ADD StoreID INT NULL;
ALTER TABLE SalesOrder ADD SalesRepID INT NULL;
ALTER TABLE SalesOrder ADD DiscountAmount DECIMAL(18,2) DEFAULT 0;
GO

ALTER TABLE SalesOrder ADD NetAmount AS (TotalAmount - DiscountAmount) PERSISTED;

ALTER TABLE SalesOrder ADD CONSTRAINT FK_SalesOrder_SalesChannel
Expand All @@ -84,13 +86,16 @@ ALTER TABLE SalesOrder ADD CONSTRAINT FK_SalesOrder_SalesRep

-- Add discount tracking to SalesOrderDetail
ALTER TABLE SalesOrderDetail ADD DiscountPercent DECIMAL(5,2) DEFAULT 0;
ALTER TABLE SalesOrderDetail ADD DiscountAmount AS (LineTotal * DiscountPercent / 100) PERSISTED;
ALTER TABLE SalesOrderDetail ADD NetAmount AS (LineTotal - (LineTotal * DiscountPercent / 100)) PERSISTED;
GO

ALTER TABLE SalesOrderDetail ADD DiscountAmount AS ((Quantity * UnitPrice) * DiscountPercent / 100) PERSISTED;
ALTER TABLE SalesOrderDetail ADD NetAmount AS ((Quantity * UnitPrice) - ((Quantity * UnitPrice) * DiscountPercent / 100)) PERSISTED;

-- Add customer segmentation
ALTER TABLE Customer ADD CustomerType NVARCHAR(20) DEFAULT 'Retail'; -- Retail, Wholesale, Online
ALTER TABLE Customer ADD SalesRepID INT NULL;
ALTER TABLE Customer ADD TerritoryID INT NULL;
GO

ALTER TABLE Customer ADD CONSTRAINT FK_Customer_SalesRep
FOREIGN KEY (SalesRepID) REFERENCES SalesRep(SalesRepID);
Expand Down
11 changes: 7 additions & 4 deletions samples/databases/futon-manufacturing/07-sales-reports.sql
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ GO

CREATE OR ALTER VIEW vw_Sales_ReturnsAnalysis AS
SELECT
sr.ReturnID,
DATEPART(YEAR, sr.ReturnDate) AS Year,
DATEPART(MONTH, sr.ReturnDate) AS Month,
rr.ReasonCode,
rr.ReasonDescription,
c.CustomerName,
c.CustomerType,
sc.ChannelName,
COUNT(DISTINCT sr.ReturnID) AS ReturnCount,
SUM(sr.RefundAmount) AS TotalRefunds,
AVG(sr.RefundAmount) AS AvgRefundAmount,
SUM(sr.RestockingFee) AS TotalRestockingFees,
Expand All @@ -326,6 +326,7 @@ INNER JOIN SalesChannel sc ON so.SalesChannelID = sc.SalesChannelID
INNER JOIN SalesReturnDetail srd ON sr.ReturnID = srd.ReturnID
INNER JOIN Items i ON srd.ItemID = i.ItemID
GROUP BY
sr.ReturnID,
DATEPART(YEAR, sr.ReturnDate),
DATEPART(MONTH, sr.ReturnDate),
rr.ReasonCode,
Expand Down Expand Up @@ -709,6 +710,7 @@ SELECT
ChannelName,
CustomerType,
ProductMix,
UniqueProducts,
COUNT(*) AS OrderCount,
SUM(OrderValue) AS TotalRevenue,
AVG(OrderValue) AS AvgOrderValue,
Expand All @@ -718,7 +720,8 @@ FROM ProductMix
GROUP BY
ChannelName,
CustomerType,
ProductMix;
ProductMix,
UniqueProducts;
GO

-- =============================================
Expand Down Expand Up @@ -827,8 +830,8 @@ SELECT
Value,
LAG(Count) OVER (ORDER BY StageOrder) AS PriorStageCount,
LAG(Value) OVER (ORDER BY StageOrder) AS PriorStageValue,
CAST(Count * 100.0 / NULLIF(LAG(Count) OVER (ORDER BY StageOrder), 0) AS DECIMAL(5,2)) AS ConversionRate,
CAST(Value * 100.0 / NULLIF(LAG(Value) OVER (ORDER BY StageOrder), 0) AS DECIMAL(5,2)) AS ValueRetentionRate
CAST(Count * 100.0 / NULLIF(LAG(Count) OVER (ORDER BY StageOrder), 0) AS DECIMAL(10,2)) AS ConversionRate,
CAST(Value * 100.0 / NULLIF(LAG(Value) OVER (ORDER BY StageOrder), 0) AS DECIMAL(10,2)) AS ValueRetentionRate
FROM PipelineMetrics;
GO

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ SELECT
ItemName,
SUM(UnitsReturned) AS TotalReturned,
CAST(SUM(TotalRefunds) AS DECIMAL(18,2)) AS RefundAmount,
STRING_AGG(DISTINCT ReasonDescription, ', ') AS ReturnReasons
STRING_AGG(ReasonDescription, ', ') AS ReturnReasons
FROM vw_Sales_ReturnsAnalysis
GROUP BY ItemName
ORDER BY SUM(UnitsReturned) DESC;
Expand Down