Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.90 KB | None | 0 0
  1. //Every month customer has its own status. It's stored in CustomerStatusMonth.
  2. //Sometimes we are trying to find out the life time value of the customer grouping by status:
  3. select
  4.     ReportMonth = CustomerStatusMonth.DateID,
  5.     CustomerStatus = Status.StatusName,
  6.     CustomerNumber = count(distinct CustomerStatusMonth.CustomerID),
  7.     RollingAmount = sum(InvoiceD.Amount),
  8.     CustomerActiveMonth = count(distinct CustomerStatusMonth.CustomerID + eomonth(InvoiceH.DateID)),
  9.     AVGLifeTime = CustomerActiveMonth / CustomerNumber,
  10.     AVGAmount = RollingAmount / CustomerNumber
  11. from CustomerStatusMonth
  12.  
  13.     inner join InvoiceH
  14.     on InvoiceH.CustomerID = CustomerStatusMonth.CustomerID
  15.     and InvoiceH.DateID <= CustomerStatusMonth.DateID
  16.  
  17.     inner join InvoiceD
  18.     on InvoiceH.InvoiceID = InvoiceD.InvoiceID
  19.  
  20.     left join Status
  21.     on Status.StatusID = CustomerStatusMonth.StatusID
  22.  
  23. where CustomerStatusMonth.DateID = 20171231
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement