Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.32 KB | None | 0 0
  1. SELECT DATEPART(yyyy, DATEADD(mm, 3, mydate)) AS FY
  2.  
  3.  
  4.  
  5. -- Step 1: Just the relevant fields
  6. select userid, orderid, orderdate,  
  7. revenue = sum(extprice), ordersequence = row_number() over (partition by userid order by orderdate),
  8. FirstOrderDate = cast(null as datetime)
  9. into #t
  10. FROM [FSAStore_DW].[dbo].[Fact_Sales]
  11. where OrderStatusID  in (1,3,5,21)
  12. and orderitemtype in ('Product','Shipping','Coupon','Discount')
  13. and userid is not null
  14. and StoreID = 1
  15. group by userid, orderid, orderdate
  16.  
  17. -- Step 2: Customer lifetime stats
  18.  
  19.  
  20. select userid, LTO = count(distinct orderid), LTR = sum(revenue), FOD = min(orderdate)
  21. into #t1
  22. from #t
  23. group by userid
  24.  
  25. -- Step 3: Update FOD
  26. update #t
  27. set FirstOrderDate = b.FOD
  28. from #t a inner join #t1 b
  29. on a.UserID = b.UserID
  30.  
  31. -- Step 4: Aggregates
  32. select
  33. AcquisitionPeriod = DATEPART(yyyy, DATEADD(mm, -3, FirstOrderDate)),
  34. Customers = count(distinct userid),
  35.  
  36. Retention12M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 365 and ordersequence > 1 then userid else null end),
  37. Revenue12M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 365 then Revenue else 0 end),
  38. Orders12M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 365 then orderid else null end),
  39. ValuePerCust12M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 365 then Revenue else 0 end)/count(distinct userid),
  40.  
  41. Retention18M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 545 and ordersequence > 1 then userid else null end),
  42. Revenue18M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 545 then Revenue else 0 end),
  43. Orders18M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 545 then orderid else null end),
  44. ValuePerCust18M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 545 then Revenue else 0 end)/count(distinct userid),
  45.  
  46.  
  47. Retention24M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 730 and ordersequence > 1 then userid else null end),
  48. Revenue24M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 730 then Revenue else 0 end),
  49. Orders24M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 730 then orderid else null end),
  50. ValuePerCust24M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 730 then Revenue else 0 end)/count(distinct userid),
  51.  
  52. Retention36M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 1095 and ordersequence > 1 then userid else null end),
  53. Revenue36M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 1095 then Revenue else 0 end),
  54. Orders36M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 1095 then orderid else null end),
  55. ValuePerCust36M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 1095 then Revenue else 0 end)/count(distinct userid),
  56.  
  57. Retention48M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 1460 and ordersequence > 1 then userid else null end),
  58. Revenue48M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 1460 then Revenue else 0 end),
  59. Orders48M = count(distinct case when datediff(dd,firstorderdate,orderdate) between 0 and 1460 then orderid else null end),
  60. ValuePerCust48M = sum(case when datediff(dd,firstorderdate,orderdate) between 0 and 1460 then Revenue else 0 end)/count(distinct userid),
  61.  
  62. RetentionTD = count(distinct case when ordersequence > 1 then userid else null end),
  63. OrdersTD = count(distinct orderid),
  64. RevenueTD = sum(revenue),
  65. ValueTD = sum(revenue)/count(distinct userid),
  66. AllCustomers = CAST(0 AS INT)
  67. INTO #report1
  68. from #t
  69. group by DATEPART(yyyy, DATEADD(mm, -3, FirstOrderDate))
  70. order by DATEPART(yyyy, DATEADD(mm, -3, FirstOrderDate))
  71.  
  72.  
  73. select
  74. Period = DATEPART(yyyy, DATEADD(mm, -3, OrderDate)),
  75. Customers = count(distinct userid)
  76. INTO #report2
  77. from #t
  78. group by DATEPART(yyyy, DATEADD(mm, -3, OrderDate))
  79. order by DATEPART(yyyy, DATEADD(mm, -3, OrderDate))
  80.  
  81. UPDATE #report1
  82. SET AllCustomers = b.Customers
  83. FROM #report1 a INNER JOIN #report2 b
  84. on a.AcquisitionPeriod  = b.period
  85.  
  86. --- Step 5 FINAL RESULT
  87. TRUNCATE TABLE dbo.LTV_RetentionReport_Ext
  88. INSERT INTO dbo.LTV_RetentionReport_Ext
  89.  
  90. SELECT * FROM #report1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement