festinko

Untitled

Sep 12th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.04 KB | None | 0 0
  1. WITH
  2.  
  3. /*List of all successful subscription payments - Alltime*/
  4.     SuccessSubsPayment AS(
  5.          SELECT
  6.             userID,
  7.             paymentDateTime,
  8.             paymentAmount,
  9.             paymentPeriod,
  10.             productID
  11.  
  12.          FROM `uptimerobot-001.ur.payments`
  13.          WHERE paymentType = 2 AND paymentStatus = 1
  14.      ),
  15.  
  16. /*Last successful subscription payments by user*/
  17.      LastSuccessSubsPayment AS(
  18.          SELECT
  19.             p1.userID,
  20.             p1.paymentDateTime as lastPaymentDateTime,
  21.             p1.paymentAmount as lastPaymentAmount,
  22.             p1.paymentPeriod as lastPaymentPeriod,
  23.             p1.productID as lastPaymentProductID
  24.  
  25.         FROM SuccessSubsPayment p1
  26.         LEFT JOIN SuccessSubsPayment p2
  27.         ON (p1.userID = p2.userID AND p1.paymentDateTime < p2.paymentDateTime)
  28.         WHERE
  29.             p2.userID IS NULL
  30.  
  31.      ),
  32.  
  33. /*added userType - Current / Churned Customer*/
  34.     LastSubsPaymentUsersType AS (
  35.             SELECT
  36.                 userID,
  37.                 lastPaymentDateTime,
  38.                 lastPaymentAmount,
  39.                 lastPaymentPeriod,
  40.                 (CASE
  41.                         WHEN (DATE_ADD(CAST(lastPaymentDateTime AS DATETIME), INTERVAL lastPaymentPeriod MONTH) >= CURRENT_DATE()) THEN 'CurrentCustomer'
  42.                         ELSE 'ChurnedCustomer'
  43.                 END) AS customerType,
  44.                 lastPaymentProductID
  45.             FROM LastSuccessSubsPayment
  46.     ),
  47.  
  48. /*added productName*/
  49.     LastSubsPaymentUsersTypeProduct AS (
  50.             SELECT
  51.                 lsput.userID,
  52.                 lsput.lastPaymentDateTime,
  53.                 lsput.lastPaymentAmount,
  54.                 lsput.lastPaymentPeriod,
  55.                 lsput.customerType,
  56.                 lsput.lastPaymentProductID,
  57.                 pr.productName
  58.  
  59.             FROM
  60.                 LastSubsPaymentUsersType as lsput
  61.             LEFT JOIN `uptimerobot-001.ur.products` as pr
  62.                 ON pr.productID = lsput.lastPaymentProductID
  63.     ),
  64.  
  65. /*Deduplication of multipla last successful payments from the same user - some edge cases*/
  66.   LastSuccessSubsPaymentUserTypeProduct AS (
  67.         SELECT
  68.             userID,
  69.             MAX(lastPaymentDateTime) as lastPaymentDateTime,
  70.             MAX(lastPaymentAmount) as lastPaymentAmount,
  71.             MAX(lastPaymentPeriod) as lastPaymentPeriod,
  72.             MAX(customerType) as customerType,
  73.             MAX(lastPaymentProductID) as lastPaymentProductID,
  74.             MAX(productName) as lastPaymentProductName
  75.        
  76.         FROM LastSubsPaymentUsersTypeProduct
  77.        
  78.         GROUP BY userID
  79.         ORDER BY userID DESC
  80.         )
  81.  
  82. SELECT
  83.     customerType,
  84.     lastPaymentProductName,
  85.     count(userID) as userCount,
  86.     ROUND(SUM(lastPaymentAmount)) as Revenue,
  87.     ROUND(SUM(lastPaymentAmount/lastPaymentPeriod)) as MRR
  88.  
  89. FROM
  90.     LastSuccessSubsPaymentUserTypeProduct
  91.  
  92. WHERE lastPaymentPeriod IS NOT NULL AND lastPaymentPeriod > 0
  93.  
  94. GROUP BY
  95.     customerType,
  96.     lastPaymentProductName
Add Comment
Please, Sign In to add comment