-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVIEW-vProductSalesSummary.sql
More file actions
41 lines (39 loc) · 1004 Bytes
/
VIEW-vProductSalesSummary.sql
File metadata and controls
41 lines (39 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- =============================================
-- Create View for Customer Segmentation:
-- Recency, Frequency, Monetary
-- =============================================
USE AdventureWorks2019
GO
IF object_id(N'dbo.vProductSalesSummary', 'V') IS NOT NULL
DROP VIEW dbo.vProductSalesSummary
GO
CREATE VIEW dbo.vProductSalesSummary AS
WITH sale AS (
SELECT SalesOrderID
, ProductID
, OrderQty
, UnitPrice * (1 - UnitPriceDiscount) AS FinalUnitPrice
, LineTotal
FROM Sales.SalesOrderDetail
),
products AS (
SELECT p.ProductID
, p.Name AS ProductName
, s.ProductSubcategoryID
, s.Name AS SubcatName
, c.ProductCategoryID
, c.Name AS CatName
FROM Production.Product p
LEFT JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
LEFT JOIN Production.ProductCategory c
ON s.ProductCategoryID = c.ProductCategoryID
)
SELECT s.*
, p.ProductName
, p.SubcatName
, p.CatName
FROM sale s
LEFT JOIN products p
ON s.ProductID = p.ProductID
GO