-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVIEW-vCustomerSalesSummary.sql
41 lines (39 loc) · 1.22 KB
/
VIEW-vCustomerSalesSummary.sql
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 Sales Summary:
-- Days Since Last Transaction, Number of Transactions, Total Spending
-- =============================================
USE AdventureWorks2019
GO
IF object_id(N'dbo.vCustomerSalesSummary', 'V') IS NOT NULL
DROP VIEW dbo.vCustomerSalesSummary
GO
CREATE VIEW dbo.vCustomerSalesSummary AS
WITH RawSales AS (
SELECT CustomerID
, SUM(TotalDue) AS TotalSpending
, COUNT(DISTINCT SalesOrderID) AS NumOfTxn
, MIN(OrderDate) AS FirstDate
, MAX(OrderDate) AS LastDate
, DATEDIFF(DAY, MIN(OrderDate), DATEADD(DAY, 1, MAX(OrderDate))) AS ActiveDays
FROM Sales.SalesOrderHeader
GROUP BY CustomerID
),
Customers AS (
SELECT c.CustomerID
, CONCAT(ISNULL(p.Title + ' ', ''), p.FirstName, ' ', p.LastName) AS FullName
FROM Sales.Customer c
INNER JOIN Person.Person p
ON c.PersonID = p.BusinessEntityID
)
SELECT RawSales.*
, ActiveWeeks =
CASE
WHEN CAST(RawSales.ActiveDays / 7 AS INT) = 0 THEN 1
ELSE CAST(RawSales.ActiveDays / 7 AS INT)
END
, DATEDIFF(DAY, RawSales.LastDate, '2014-07-01T00:00:00.000') AS DaysSinceLastTxn
, Customers.FullName
FROM RawSales
INNER JOIN Customers
ON RawSales.CustomerID = Customers.CustomerID;
GO